SpringBoot整合Shiro
创建项目 整合mybaits 整合shiro 整合前端html 使用
创建项目
最终项目结构 引入依赖 pom.xml
< dependencies>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- web< / artifactId>
< / dependency>
< dependency>
< groupId> org. apache. shiro< / groupId>
< artifactId> shiro- spring- boot- web- starter< / artifactId>
< version> 1.5 .3 < / version>
< / dependency>
< ! -- mybatis- springboot- starter-- >
< dependency>
< groupId> org. mybatis. spring. boot< / groupId>
< artifactId> mybatis- spring- boot- starter< / artifactId>
< version> 2.2 .2 < / version>
< / dependency>
< ! -- mysql 依赖 -- >
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql- connector- java< / artifactId>
< / dependency>
< ! -- 导入thymeleaf依赖-- >
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- thymeleaf< / artifactId>
< / dependency>
< ! -- shiro- thymeleaf整合-- >
< dependency>
< groupId> com. github. theborakompanioni< / groupId>
< artifactId> thymeleaf- extras- shiro< / artifactId>
< version> 2.1 .0 < / version>
< / dependency>
< / dependencies>
整合mybaits
设置配置文件application.yml,配置mysql数据库,设置mybatis的实体和mapper.xml的目录
spring :
datasource :
username : root
password : 123456
url : jdbc: mysql: / / localhost: 3306 / shiro? serverTimezone= UTC & useUnicode= true & characterEncoding= utf- 8
driver- class - name: com. mysql. cj. jdbc. Driver
mybatis :
type- aliases- package : org. shiro. entity
mapper- locations: classpath: mapper
CREATE TABLE ` user ` (
` id ` int ( 20 ) NOT NULL AUTO_INCREMENT ,
` name ` varchar ( 30 ) DEFAULT NULL ,
` pwd ` varchar ( 30 ) DEFAULT NULL ,
` perms ` varchar ( 50 ) DEFAULT NULL ,
PRIMARY KEY ( ` id ` )
) ENGINE = InnoDB AUTO_INCREMENT = 2 DEFAULT CHARSET = utf8;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String name;
private String pwd;
private String perms;
}
< ? xml version= "1.0" encoding= "UTF-8" ? >
< ! DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace= "org.shiro.dao.UserMapper" >
< select id= "queryUserByName" parameterType= "String" resultType= "org.shiro.entity.User" >
select * from user where name = #{ name} ;
< / select>
< / mapper>
@Repository
@Mapper
public interface UserMapper {
public User queryUserByName ( String name) ;
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User queryUserByName ( String name) {
return userMapper. queryUserByName ( name) ;
}
}
public interface UserService {
public User queryUserByName ( String name) ;
}
整合shiro
public class UserRealm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo ( PrincipalCollection principalCollection ) {
System. out. println ( "执行了授权" ) ;
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo ( ) ;
Subject subject = SecurityUtils. getSubject ( ) ;
User currentUser = ( User) subject. getPrincipal ( ) ;
System. out. println ( currentUser. getPerms ( ) ) ;
info. addStringPermission ( currentUser. getPerms ( ) ) ;
return info;
}
@Autowired
private UserService userService;
@Override
protected AuthenticationInfo doGetAuthenticationInfo ( AuthenticationToken authenticationToken) throws AuthenticationException {
System. out. println ( "执行了认证" ) ;
UsernamePasswordToken userToken = ( UsernamePasswordToken) authenticationToken;
User user = userService. queryUserByName ( userToken. getUsername ( ) ) ;
if ( user == null ) {
return null ;
}
return new SimpleAuthenticationInfo ( user, user. getPwd ( ) , "" ) ;
}
}
@Configuration
public class ShiroConfig {
@Bean ( name = "shiroFilterFactoryBean" )
public ShiroFilterFactoryBean getShiroFilterBean ( @Qualifier ( "securityManager" ) DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean ( ) ;
bean. setSecurityManager ( defaultWebSecurityManager) ;
Map< String, String> filterMap = new LinkedHashMap < > ( ) ;
filterMap. put ( "/user/add" , "authc" ) ;
filterMap. put ( "/user/update" , "authc" ) ;
filterMap. put ( "/user/add" , "perms[user:add]" ) ;
filterMap. put ( "/user/update" , "perms[user:update]" ) ;
bean. setFilterChainDefinitionMap ( filterMap) ;
bean. setLoginUrl ( "/toLogin" ) ;
bean. setUnauthorizedUrl ( "/noauth" ) ;
return bean;
}
@Bean ( name = "securityManager" )
public DefaultWebSecurityManager getDefaultWebSecurityManager ( @Qualifier ( "userRealm" ) UserRealm userRealm) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager ( ) ;
securityManager. setRealm ( userRealm) ;
return securityManager;
}
@Bean ( name = "userRealm" )
public UserRealm userRealm ( ) {
return new UserRealm ( ) ;
}
}
@Controller
public class HelloController {
@RequestMapping ( { "/" , "/index" } )
public String toIndex ( Model model) {
model. addAttribute ( "msg" , "hello" ) ;
return "index" ;
}
@RequestMapping ( "/user/add" )
public String add ( ) {
return "/user/add" ;
}
@RequestMapping ( "/user/update" )
public String update ( ) {
return "/user/update" ;
}
@RequestMapping ( "/toLogin" )
public String toLogin ( ) {
return "login" ;
}
@RequestMapping ( "/noauth" )
@ResponseBody
public String unauthorized ( ) {
return "未授权无法访问此页面" ;
}
@RequestMapping ( "/login" )
public String login ( String username, String password, Model model) {
Subject subject = SecurityUtils . getSubject ( ) ;
UsernamePasswordToken token = new UsernamePasswordToken ( username, password) ;
try {
subject. login ( token) ;
return "index" ;
} catch ( UnknownAccountException e) {
model. addAttribute ( "msg" , "用户名不存在!" ) ;
return "login" ;
} catch ( IncorrectCredentialsException e) {
model. addAttribute ( "msg" , "密码错误!" ) ;
return "login" ;
}
}
}
整合前端html
< ! DOCTYPE html>
< html lang= "en" xmlns: th= "http://www.thymeleaf.org" >
< head>
< meta charset= "UTF-8" >
< title> Title < / title>
< / head>
< body>
< h1> 登录< / h1>
< hr>
< form th: action= "@{/login}" >
用户名:< input type= "text" name= "username" > < br>
密码:< input type= "password" name= "password" >
< br>
< input type= "submit" name= "提交" >
< / form>
< / body>
< / html>
创建index.html, shiro:hasPermission:有权限显示
< ! DOCTYPE html>
< html lang= "en" xmlns: th= "http://www.thymeleaf.org"
xmlns: shiro= "http://www.thymeleaf.org/thymeleaf-extras-shiro" >
< head>
< meta charset= "UTF-8" >
< title> Title < / title>
< / head>
< body>
< h1> 首页< / h1>
< div th: if = "${session.loginUser==null}" >
< a th: href= "@{/toLogin}" > 登录< / a>
< / div>
< p th: text= "${msg}" > < / p>
< hr>
< div shiro: hasPermission= "user:add" >
< a th: href= "@{/user/add}" > add< / a>
< / div>
< div shiro: hasPermission= "user:update" >
< a th: href= "@{/user/update}" > update< / a>
< / div>
< a th: href= "@{/logout}" > 注销< / a>
< / body>
< / html>
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> Title < / title>
< / head>
< body>
< h> add< / h>
< / body>
< / html>
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> Title < / title>
< / head>
< body>
< h> update < / h>
< / body>
< / html>
使用
数据库添加一条数据 访问登录页http://localhost:8080/toLogin ,输入账号密码,提交 进入首页 权限设置user:update,所以只显示update 点击update,进入update页