Skip to content

Commit eba9755

Browse files
springboot整合shiro
1 parent f82704e commit eba9755

14 files changed

Lines changed: 650 additions & 13 deletions

File tree

logs/all.log

Lines changed: 235 additions & 0 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@
104104
<groupId>org.springframework.boot</groupId>
105105
<artifactId>spring-boot-starter-thymeleaf</artifactId>
106106
</dependency>
107+
<dependency>
108+
<groupId>org.apache.shiro</groupId>
109+
<artifactId>shiro-spring</artifactId>
110+
<version>1.3.2</version>
111+
</dependency>
107112

108113

109114
</dependencies>

src/main/java/com/xh/basic/bean/Resp.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ public static <T>Resp success(){
140140
return new Resp<T>(SUCCESS_CODE, "请求成功", null);
141141
}
142142

143+
public static <T>Resp success(String message){
144+
return new Resp<T>(SUCCESS_CODE, message);
145+
}
146+
143147
public static <T>Resp success(T result){
144148
return new Resp<T>(SUCCESS_CODE, "请求成功", result);
145149
}
Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,87 @@
11
package com.xh.basic.config;
22

3+
import com.xh.basic.shiro.CustomRealm;
4+
import org.apache.shiro.mgt.SecurityManager;
5+
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
6+
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
import java.util.LinkedHashMap;
13+
import java.util.Map;
14+
315
/**
416
* @author szq
517
* @Package com.xh.basic.config
6-
* @Description: to do ...
18+
* @Description: Shiro配置类
719
* @date 2018/5/214:36
820
*/
21+
@Configuration
922
public class ShiroConfig {
23+
24+
private Logger logger = LoggerFactory.getLogger(ShiroConfig.class);
25+
/**
26+
* 过滤器默认权限表 {anon=anon, authc=authc, authcBasic=authcBasic, logout=logout,
27+
* noSessionCreation=noSessionCreation, perms=perms, port=port,
28+
* rest=rest, roles=roles, ssl=ssl, user=user}
29+
* <p>
30+
* anon, authc, authcBasic, user 是第一组认证过滤器
31+
* perms, port, rest, roles, ssl 是第二组授权过滤器
32+
* <p>
33+
* user 和 authc 的不同:当应用开启了rememberMe时, 用户下次访问时可以是一个user, 但绝不会是authc,
34+
* 因为authc是需要重新认证的, user表示用户不一定已通过认证, 只要曾被Shiro记住过登录状态的用户就可以正常发起请求,比如rememberMe
35+
* 以前的一个用户登录时开启了rememberMe, 然后他关闭浏览器, 下次再访问时他就是一个user, 而不会authc
36+
*
37+
* @param securityManager 初始化 ShiroFilterFactoryBean 的时候需要注入 SecurityManager
38+
*/
39+
@Bean
40+
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
41+
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
42+
//必须设置 SecurityManager
43+
shiroFilterFactoryBean.setSecurityManager(securityManager);
44+
//setLoginUrl 默认会自动寻找web工程根目录下的“/login.jsp”页面或“/login”映射
45+
shiroFilterFactoryBean.setLoginUrl("/notLogin");
46+
//设置无权限时跳转的url
47+
shiroFilterFactoryBean.setUnauthorizedUrl("/notRole");
48+
49+
//设置拦截器
50+
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
51+
//游客,开发权限
52+
filterChainDefinitionMap.put("/guest/**", "anon");
53+
//用户,需要角色权限“user”
54+
filterChainDefinitionMap.put("/user/**", "roles[user]");
55+
//管理员,需要角色权限“admin”
56+
filterChainDefinitionMap.put("/admin/**", "roles[admin]");
57+
//开发登录接口
58+
filterChainDefinitionMap.put("/login", "anon");
59+
//其余接口一律拦截
60+
//这行代码必须放在所有权限设置的最后,不然会导致所有url都被拦截
61+
filterChainDefinitionMap.put("/**", "anon");
62+
63+
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
64+
logger.info("Shiro拦截器工厂类注入成功!");
65+
return shiroFilterFactoryBean;
66+
}
67+
68+
/**
69+
* 注入securityManager
70+
*/
71+
@Bean
72+
public SecurityManager securityManager(){
73+
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
74+
//设置realm
75+
securityManager.setRealm(customRealm());
76+
return securityManager;
77+
}
78+
79+
/**
80+
* 自定义身份认证 realm
81+
* 必须写这个类,并加上@Bean注解,目的是注入CustomRealm,否则会影响CustomRealm类中其他类的依赖注入
82+
*/
83+
@Bean
84+
public CustomRealm customRealm(){
85+
return new CustomRealm();
86+
}
1087
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
package com.xh.basic.controller;
22

3+
import com.xh.basic.bean.Resp;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
import org.springframework.web.bind.annotation.RestController;
7+
38
/**
49
* @author szq
510
* @Package com.xh.basic.controller
6-
* @Description: to do ...
11+
* @Description: 管理员
712
* @date 2018/5/215:42
813
*/
14+
@RestController
15+
@RequestMapping("/admin")
916
public class AdminController {
17+
18+
@RequestMapping(value = "/getMessage", method = RequestMethod.GET)
19+
public Resp getMessage(){
20+
return new Resp().success("您拥有管理员权限,可以获得该接口的信息!");
21+
}
1022
}
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
package com.xh.basic.controller;
22

3+
import com.xh.basic.bean.Resp;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
import org.springframework.web.bind.annotation.RestController;
7+
38
/**
49
* @author szq
510
* @Package com.xh.basic.controller
6-
* @Description: to do ...
11+
* @Description: 游客
712
* @date 2018/5/215:36
813
*/
14+
@RestController
15+
@RequestMapping("/guest")
916
public class GuestController {
17+
18+
@RequestMapping(value = "/enter", method = RequestMethod.GET)
19+
public Resp login(){
20+
return new Resp().success("欢迎进入,您的身份是游客。");
21+
}
22+
23+
@RequestMapping(value = "/getMessage", method = RequestMethod.GET)
24+
public Resp submitLogin(){
25+
return new Resp().success("您用户获得该接口的信息的权限");
26+
}
1027
}
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
11
package com.xh.basic.controller;
22

3+
import com.xh.basic.bean.Resp;
4+
import com.xh.basic.dao.UserMapper;
5+
import org.apache.shiro.SecurityUtils;
6+
import org.apache.shiro.authc.UsernamePasswordToken;
7+
import org.apache.shiro.subject.Subject;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.bind.annotation.RestController;
12+
313
/**
414
* @author szq
515
* @Package com.xh.basic.controller
6-
* @Description: to do ...
16+
* @Description: 登陆
717
* @date 2018/5/215:43
818
*/
19+
@RestController
920
public class LoginController {
21+
22+
private final UserMapper userMapper;
23+
24+
@Autowired
25+
public LoginController(UserMapper userMapper){
26+
this.userMapper = userMapper;
27+
}
28+
29+
@RequestMapping(value = "/notLogin", method = RequestMethod.GET)
30+
public Resp notLogin(){
31+
return new Resp().success("您尚未登陆!");
32+
}
33+
34+
@RequestMapping(value = "/notRole", method = RequestMethod.GET)
35+
public Resp notRole(){
36+
return new Resp().success("您没有权限!");
37+
}
38+
39+
@RequestMapping(value = "/logout", method = RequestMethod.GET)
40+
public Resp logout(){
41+
Subject subject = SecurityUtils.getSubject();
42+
//注销
43+
subject.logout();
44+
return new Resp().success("成功注销!");
45+
}
46+
47+
@RequestMapping(value = "/login", method = RequestMethod.POST)
48+
public Resp login(String username, String password){
49+
//从SecurityUtils里边创建一个subject
50+
Subject subject = SecurityUtils.getSubject();
51+
//在认证提交前准备token令牌
52+
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
53+
//执行认证登陆
54+
subject.login(token);
55+
//根据权限指定返回数据
56+
String role = userMapper.getRole(username);
57+
if ("user".equals(role)){
58+
return new Resp().success("欢迎登陆");
59+
}
60+
if ("admin".equals(role)){
61+
return new Resp().success("欢迎来到管理员页面");
62+
}
63+
return new Resp().success("权限错误");
64+
}
1065
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
package com.xh.basic.controller;
22

3+
import com.xh.basic.bean.Resp;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
import org.springframework.web.bind.annotation.RestController;
7+
38
/**
49
* @author szq
510
* @Package com.xh.basic.controller
6-
* @Description: to do ...
11+
* @Description: 普通登录用户
712
* @date 2018/5/215:41
813
*/
14+
@RestController
15+
@RequestMapping("/user")
916
public class UserController {
17+
18+
@RequestMapping(value = "/getMessage", method = RequestMethod.GET)
19+
public Resp getMessage() {
20+
return new Resp().success("您拥有用户权限,可以获得该接口的信息!");
21+
}
22+
1023
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.xh.basic.dao;
2+
3+
import com.xh.basic.model.User;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
public interface UserMapper {
8+
int deleteByPrimaryKey(String id);
9+
10+
int insert(User record);
11+
12+
int insertSelective(User record);
13+
14+
User selectByPrimaryKey(String id);
15+
16+
int updateByPrimaryKeySelective(User record);
17+
18+
int updateByPrimaryKey(User record);
19+
20+
String getRole(String username);
21+
22+
String getPassword(String username);
23+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.xh.basic.model;
2+
3+
public class User {
4+
private String id;
5+
6+
private String username;
7+
8+
private String password;
9+
10+
private String role;
11+
12+
public String getId() {
13+
return id;
14+
}
15+
16+
public void setId(String id) {
17+
this.id = id == null ? null : id.trim();
18+
}
19+
20+
public String getUsername() {
21+
return username;
22+
}
23+
24+
public void setUsername(String username) {
25+
this.username = username == null ? null : username.trim();
26+
}
27+
28+
public String getPassword() {
29+
return password;
30+
}
31+
32+
public void setPassword(String password) {
33+
this.password = password == null ? null : password.trim();
34+
}
35+
36+
public String getRole() {
37+
return role;
38+
}
39+
40+
public void setRole(String role) {
41+
this.role = role == null ? null : role.trim();
42+
}
43+
}

0 commit comments

Comments
 (0)