Skip to content

Commit b6917e6

Browse files
committed
[update]添加spring boot shiro 相关代码
1 parent 59cf3e3 commit b6917e6

File tree

18 files changed

+613
-142
lines changed

18 files changed

+613
-142
lines changed

spring-boot-shiro/pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@
8383
<artifactId>pagehelper-spring-boot-starter</artifactId>
8484
<version>1.2.3</version>
8585
</dependency>
86+
<dependency>
87+
<groupId>net.sourceforge.nekohtml</groupId>
88+
<artifactId>nekohtml</artifactId>
89+
</dependency>
90+
91+
<!-- fastjson -->
92+
<dependency>
93+
<groupId>com.alibaba</groupId>
94+
<artifactId>fastjson</artifactId>
95+
<version>1.2.31</version>
96+
</dependency>
8697

8798
<dependency>
8899
<groupId>org.springframework.boot</groupId>

spring-boot-shiro/src/main/java/com/example/springbootshiro/config/ShiroConfig.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,41 @@ public class ShiroConfig {
1818
@Bean
1919
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
2020
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
21-
2221
shiroFilterFactoryBean.setSecurityManager(securityManager);
23-
24-
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
25-
//配置退出过滤器
26-
filterChainDefinitionMap.put("/logout", "logout");
27-
28-
filterChainDefinitionMap.put("/**", "authc");
29-
30-
//登录界面
3122
shiroFilterFactoryBean.setLoginUrl("/login");
32-
//登录成功后的跳转界面
3323
shiroFilterFactoryBean.setSuccessUrl("/index");
34-
//未授权界面
3524
shiroFilterFactoryBean.setUnauthorizedUrl("/403");
3625

26+
LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
27+
28+
filterChainDefinitionMap.put("/css/**", "anon");
29+
filterChainDefinitionMap.put("/js/**", "anon");
30+
filterChainDefinitionMap.put("/fonts/**", "anon");
31+
filterChainDefinitionMap.put("/img/**", "anon");
32+
filterChainDefinitionMap.put("/druid/**", "anon");
33+
filterChainDefinitionMap.put("/user/regist", "anon");
34+
filterChainDefinitionMap.put("/gifCode", "anon");
35+
filterChainDefinitionMap.put("/logout", "logout");
36+
filterChainDefinitionMap.put("/", "anon");
37+
filterChainDefinitionMap.put("/**", "user");
38+
3739
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
40+
3841
return shiroFilterFactoryBean;
3942
}
4043

44+
@Bean
45+
public ShiroRealm myShiroRealm() {
46+
return new ShiroRealm();
47+
}
4148
/**
4249
* 注入Shiro SerucityManager
4350
* @return
4451
*/
4552
@Bean
4653
public SecurityManager securityManager(){
4754
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
55+
securityManager.setRealm(myShiroRealm());
4856
return securityManager;
4957
}
5058
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.example.springbootshiro.config;
2+
3+
import com.example.springbootshiro.domain.MenuInfo;
4+
import com.example.springbootshiro.domain.RoleInfo;
5+
import com.example.springbootshiro.domain.UserInfo;
6+
import com.example.springbootshiro.service.MenuService;
7+
import com.example.springbootshiro.service.RoleService;
8+
import com.example.springbootshiro.service.UserService;
9+
import org.apache.shiro.SecurityUtils;
10+
import org.apache.shiro.authc.*;
11+
import org.apache.shiro.authz.AuthorizationInfo;
12+
import org.apache.shiro.authz.SimpleAuthorizationInfo;
13+
import org.apache.shiro.realm.AuthorizingRealm;
14+
import org.apache.shiro.subject.PrincipalCollection;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
17+
import java.util.HashSet;
18+
import java.util.List;
19+
import java.util.Set;
20+
21+
public class ShiroRealm extends AuthorizingRealm {
22+
23+
@Autowired
24+
private UserService userService;
25+
@Autowired
26+
private RoleService roleService;
27+
@Autowired
28+
private MenuService menuService;
29+
30+
@Override
31+
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
32+
UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
33+
String userName = user.getUsername();
34+
35+
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
36+
37+
List<RoleInfo> roleList = this.roleService.findUserRole(userName);
38+
Set<String> roleSet = new HashSet<String>();
39+
for (RoleInfo r : roleList) {
40+
roleSet.add(r.getRoleName());
41+
}
42+
simpleAuthorizationInfo.setRoles(roleSet);
43+
44+
List<MenuInfo> permissionList = this.menuService.findUserPermissions(userName);
45+
Set<String> permissionSet = new HashSet<String>();
46+
for (MenuInfo m : permissionList) {
47+
permissionSet.add(m.getPerms());
48+
}
49+
simpleAuthorizationInfo.setStringPermissions(permissionSet);
50+
return simpleAuthorizationInfo;
51+
}
52+
53+
@Override
54+
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
55+
String userName = (String) token.getPrincipal();
56+
String password = new String((char[]) token.getCredentials());
57+
58+
UserInfo user = this.userService.findByName(userName);
59+
60+
if (user == null) {
61+
throw new UnknownAccountException("用户名或密码错误!");
62+
}
63+
if (!password.equals(user.getPassword())) {
64+
throw new IncorrectCredentialsException("用户名或密码错误!");
65+
}
66+
if ("0".equals(user.getStatus())) {
67+
throw new LockedAccountException("账号已被锁定,请联系管理员!");
68+
}
69+
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName());
70+
return info;
71+
}
72+
73+
}

spring-boot-shiro/src/main/java/com/example/springbootshiro/controller/LoginController.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public String login() {
3232
@PostMapping("/login")
3333
@ResponseBody
3434
public ResponseResult login(String username, String password, String code, Boolean rememberMe) {
35-
if (StringUtils.isNullorEmpty(code)) {
36-
return ResponseResult.warn("验证码不能为空!");
37-
}
35+
// if (StringUtils.isNullorEmpty(code)) {
36+
// return ResponseResult.warn("验证码不能为空!");
37+
// }
3838
// Session session = super.getSession();
3939
// String sessionCode = (String) session.getAttribute("_code");
4040
// if (!code.toLowerCase().equals(sessionCode)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.example.springbootshiro.controller;
2+
3+
import com.example.springbootshiro.domain.QueryRequest;
4+
import com.example.springbootshiro.domain.UserInfo;
5+
import com.example.springbootshiro.service.UserService;
6+
import com.github.pagehelper.PageHelper;
7+
import com.github.pagehelper.PageInfo;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.ui.Model;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.ResponseBody;
13+
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
/**
18+
* Created by Douglee on 2018/4/2.
19+
*/
20+
@Controller
21+
public class UserController extends BaseController {
22+
@Autowired
23+
private UserService userService;
24+
25+
@GetMapping("user")
26+
public String index(Model model) {
27+
UserInfo userInfo = super.getCurrentUser();
28+
model.addAttribute("user", userInfo);
29+
return "user/user";
30+
}
31+
32+
@GetMapping("user/list")
33+
@ResponseBody
34+
public Map<String, Object> userList(QueryRequest request, UserInfo userInfo) {
35+
PageHelper.startPage(request.getPageNum(), request.getPageSize());
36+
List<UserInfo> list = userService.getUserList();
37+
PageInfo<UserInfo> pageInfo = new PageInfo<>(list);
38+
39+
return getDataTable(pageInfo);
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.springbootshiro.domain;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Created by Douglee on 2018/4/2.
7+
*/
8+
public class QueryRequest implements Serializable {
9+
10+
private static final long serialVersionUID = -8510120969167698570L;
11+
12+
private int pageSize;
13+
14+
private int pageNum;
15+
16+
public int getPageSize() {
17+
return pageSize;
18+
}
19+
20+
public void setPageSize(int pageSize) {
21+
this.pageSize = pageSize;
22+
}
23+
24+
public int getPageNum() {
25+
return pageNum;
26+
}
27+
28+
public void setPageNum(int pageNum) {
29+
this.pageNum = pageNum;
30+
}
31+
}

0 commit comments

Comments
 (0)