Commit 9b210c7c489733bc3d129296b2a56ae18095e917

Authored by 王通
1 parent 617a41f8

1.加入资源验证过滤 一个Module多个资源

src/main/java/com/bsth/common/Constants.java
... ... @@ -68,4 +68,6 @@ public class Constants {
68 68 public static final String SSO_TOKEN = "ssoToken";
69 69  
70 70 public static final String SPECIAL_ROLES = "special.roles";
  71 +
  72 + public static final String RESOURCE_AUTHORITYS = "resourceAuthoritys";
71 73 }
... ...
src/main/java/com/bsth/common/Setting.java
... ... @@ -6,14 +6,26 @@ import org.springframework.stereotype.Component;
6 6 @Component
7 7 public class Setting {
8 8  
9   - @Value("${enabled.whiteip}")
10   - private boolean whiteipEnabled;
11 9  
12   - public boolean isWhiteipEnabled() {
  10 + private static boolean whiteipEnabled;
  11 +
  12 + private static boolean authorityEnabled;
  13 +
  14 + public static boolean isWhiteipEnabled() {
13 15 return whiteipEnabled;
14 16 }
15 17  
  18 + @Value("${enabled.whiteip}")
16 19 public void setWhiteipEnabled(boolean whiteipEnabled) {
17   - this.whiteipEnabled = whiteipEnabled;
  20 + Setting.whiteipEnabled = whiteipEnabled;
  21 + }
  22 +
  23 + public static boolean isAuthorityEnabled() {
  24 + return authorityEnabled;
  25 + }
  26 +
  27 + @Value("${enabled.authority}")
  28 + public void setAuthorityEnabled(boolean authorityEnabled) {
  29 + Setting.authorityEnabled = authorityEnabled;
18 30 }
19 31 }
... ...
src/main/java/com/bsth/controller/realcontrol/AdminUtilsController.java
... ... @@ -356,4 +356,17 @@ public class AdminUtilsController {
356 356  
357 357 return "error";
358 358 }
  359 +
  360 + @RequestMapping("/authoritySwitch")
  361 + public String authoritySwitch(boolean authorityEnabled) {
  362 + Map<String, Object> result = new HashMap<>();
  363 + try {
  364 + setting.setAuthorityEnabled(authorityEnabled);
  365 + return "success";
  366 + } catch (Exception e) {
  367 + e.printStackTrace();
  368 + }
  369 +
  370 + return "error";
  371 + }
359 372 }
360 373 \ No newline at end of file
... ...
src/main/java/com/bsth/controller/sys/UserController.java
... ... @@ -126,6 +126,7 @@ public class UserController extends BaseController&lt;SysUser, Integer&gt; {
126 126 //session里写入用户名,webSocket连接时标识身份用
127 127 session.setAttribute(Constants.SSO_TOKEN, token);
128 128 session.setAttribute(Constants.SESSION_USERNAME, sysUser.getUserName());
  129 + session.setAttribute(Constants.RESOURCE_AUTHORITYS, sysUser.getLinks());
129 130 //获取公司权限数据
130 131 List<CompanyAuthority> cmyAuths = companyAuthorityService.findByUser(sysUser);
131 132 session.setAttribute(Constants.COMPANY_AUTHORITYS, cmyAuths);
... ... @@ -204,6 +205,7 @@ public class UserController extends BaseController&lt;SysUser, Integer&gt; {
204 205 sysUserService.recordLoginDate(userName);
205 206 //session里写入用户名,webSocket连接时标识身份用
206 207 session.setAttribute(Constants.SESSION_USERNAME, user.getUserName());
  208 + session.setAttribute(Constants.RESOURCE_AUTHORITYS, user.getLinks());
207 209  
208 210 //获取公司权限数据
209 211 List<CompanyAuthority> cmyAuths = companyAuthorityService.findByUser(user);
... ... @@ -259,6 +261,7 @@ public class UserController extends BaseController&lt;SysUser, Integer&gt; {
259 261 SecurityUtils.login(user, request);
260 262 //session里写入用户名,webSocket连接时标识身份用
261 263 session.setAttribute(Constants.SESSION_USERNAME, user.getUserName());
  264 + session.setAttribute(Constants.RESOURCE_AUTHORITYS, user.getLinks());
262 265  
263 266 //获取公司权限数据
264 267 List<CompanyAuthority> cmyAuths = companyAuthorityService.findByUser(user);
... ...
src/main/java/com/bsth/entity/sys/SysUser.java
... ... @@ -2,10 +2,12 @@ package com.bsth.entity.sys;
2 2  
3 3 import com.fasterxml.jackson.annotation.JsonIgnore;
4 4 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  5 +import org.springframework.util.StringUtils;
5 6  
6 7 import javax.persistence.*;
7 8 import java.io.Serializable;
8 9 import java.util.Date;
  10 +import java.util.HashSet;
9 11 import java.util.LinkedHashSet;
10 12 import java.util.Set;
11 13  
... ... @@ -144,4 +146,25 @@ public class SysUser implements Serializable {
144 146 public void setRealName(String realName) {
145 147 this.realName = realName;
146 148 }
  149 +
  150 + public Set<String> getLinks() {
  151 + Set<String> links = new HashSet<>();
  152 + if (links.size() == 0) {
  153 + for (Role role : roles) {
  154 + for (Module module : role.getModules()) {
  155 + String symbol = module.getMappSymbol();
  156 + if (!StringUtils.isEmpty(symbol)) {
  157 + String[] symbols = symbol.split(";");
  158 + for (String temp : symbols) {
  159 + if (!StringUtils.isEmpty(temp)) {
  160 + links.add(temp);
  161 + }
  162 + }
  163 + }
  164 + }
  165 + }
  166 + }
  167 +
  168 + return links;
  169 + }
147 170 }
... ...
src/main/java/com/bsth/filter/AuthorityFilter.java 0 → 100644
  1 +package com.bsth.filter;
  2 +
  3 +import com.bsth.common.Constants;
  4 +import com.bsth.common.ResponseCode;
  5 +import com.bsth.common.Setting;
  6 +import com.bsth.entity.sys.SysUser;
  7 +import com.bsth.security.util.SecurityUtils;
  8 +import com.fasterxml.jackson.databind.ObjectMapper;
  9 +import org.slf4j.Logger;
  10 +import org.slf4j.LoggerFactory;
  11 +import org.springframework.beans.factory.annotation.Autowired;
  12 +import org.springframework.util.AntPathMatcher;
  13 +import org.springframework.util.PathMatcher;
  14 +
  15 +import javax.servlet.*;
  16 +import javax.servlet.http.HttpServletRequest;
  17 +import javax.servlet.http.HttpServletResponse;
  18 +import java.io.IOException;
  19 +import java.util.HashMap;
  20 +import java.util.Map;
  21 +import java.util.Set;
  22 +
  23 +/**
  24 + * 权限过滤器
  25 + * @author Hill
  26 + */
  27 +public class AuthorityFilter extends BaseFilter {
  28 +
  29 + Logger logger = LoggerFactory.getLogger(this.getClass());
  30 +
  31 + private ObjectMapper mapper = new ObjectMapper();
  32 +
  33 + private final String rootUri = "/";
  34 +
  35 + private final String scheduleReferer = "/real_control/v2";
  36 +
  37 + private String[] pubUrls = new String[]{ "/sockjs/", "/pages/", "/error", "/dictionary/all", "/user/isWeakCipher", "/user/isRealName", "/user/currentUser", "/user/companyData", "/module/findByCurrentUser", "/eci/validate_get_destroy_info", "/business", "/personnel/all_py", "/companyAuthority/all", "/line/all", "/basic/refresh_person_data", "/downloadFile", "/report/lineList" };
  38 +
  39 + @Override
  40 + public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
  41 + if (!Setting.isAuthorityEnabled()) {
  42 + chain.doFilter(request, response);
  43 + return;
  44 + }
  45 +
  46 + String uri = request.getRequestURI(), referer = request.getHeader("Referer");
  47 + Set<String> links = (Set<String>) request.getSession().getAttribute(Constants.RESOURCE_AUTHORITYS);
  48 + if (rootUri.equals(uri) || (referer != null && referer.indexOf(scheduleReferer) > 0) || isPubURL(uri)) {
  49 + chain.doFilter(request, response);
  50 + return;
  51 + }
  52 + if (links != null) {
  53 + boolean matched = false;
  54 + for (String link : links) {
  55 + if (uri.startsWith(link)) {
  56 + matched = true;
  57 + break;
  58 + }
  59 + }
  60 + if (!matched) {
  61 + Map<String, Object> result = new HashMap<>();
  62 + result.put("status", ResponseCode.ERROR);
  63 + result.put("msg", "未授权的访问");
  64 + response.setContentType("text/html;charset=utf-8");
  65 + response.getWriter().write(mapper.writeValueAsString(result));
  66 + return;
  67 + }
  68 + }
  69 +
  70 + chain.doFilter(request, response);
  71 + }
  72 +
  73 + protected boolean isPubURL(String url) {
  74 + for (String pubUrl : pubUrls) {
  75 + if (url.startsWith(pubUrl)) {
  76 + return true;
  77 + }
  78 + }
  79 +
  80 + return false;
  81 + }
  82 +}
... ...
src/main/java/com/bsth/filter/WhiteIpFilter.java
1 1 package com.bsth.filter;
2 2  
3   -import com.alibaba.fastjson.JSON;
4 3 import com.bsth.common.Setting;
5 4 import com.bsth.data.BasicData;
6 5 import com.bsth.entity.WhiteIp;
7   -import com.bsth.entity.sys.SysUser;
8   -import com.bsth.security.util.SecurityUtils;
9 6 import com.bsth.util.IpUtils;
10   -import com.google.common.collect.Lists;
11   -import com.google.common.collect.Maps;
12 7 import org.slf4j.Logger;
13 8 import org.slf4j.LoggerFactory;
14   -import org.springframework.beans.factory.annotation.Autowired;
15   -import org.springframework.core.annotation.Order;
16   -import org.springframework.stereotype.Component;
17   -import org.springframework.web.context.WebApplicationContext;
18 9  
19 10 import javax.servlet.*;
20 11 import javax.servlet.http.HttpServletRequest;
21 12 import javax.servlet.http.HttpServletResponse;
22 13 import java.io.IOException;
23   -import java.util.Enumeration;
24 14 import java.util.List;
25   -import java.util.Map;
26 15  
27 16 /**
28 17 * IP白名单过滤器
... ... @@ -32,18 +21,14 @@ public class WhiteIpFilter implements Filter {
32 21  
33 22 Logger logger = LoggerFactory.getLogger(this.getClass());
34 23  
35   - private Setting setting;
36   -
37   - public Setting getSetting() {
38   - return setting;
39   - }
40   -
41   - public void setSetting(Setting setting) {
42   - this.setting = setting;
43   - }
44   -
45 24 @Override
46 25 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  26 +
  27 + if (!Setting.isWhiteipEnabled()) {
  28 + chain.doFilter(request, response);
  29 + return;
  30 + }
  31 +
47 32 HttpServletRequest req = (HttpServletRequest)request;
48 33 HttpServletResponse res = (HttpServletResponse)response;
49 34  
... ... @@ -58,12 +43,11 @@ public class WhiteIpFilter implements Filter {
58 43 }
59 44 }
60 45 }
61   - if (isMatch || !setting.isWhiteipEnabled()) {
  46 + if (isMatch) {
62 47 chain.doFilter(request, response);
63 48 } else {
64 49 logger.info(ip + "未在白名单中,不予访问");
65 50 res.setStatus(404);
66   - return;
67 51 }
68 52 }
69 53 }
... ...
src/main/java/com/bsth/security/WebSecurityConfig.java
1 1 package com.bsth.security;
2 2  
  3 +import com.bsth.common.Constants;
3 4 import com.bsth.common.Setting;
  5 +import com.bsth.filter.AuthorityFilter;
4 6 import com.bsth.filter.WhiteIpFilter;
5   -import com.bsth.security.handler.CustomLogoutSuccessHandler;
  7 +import com.bsth.security.filter.LoginInterceptor;
6 8 import org.springframework.beans.factory.annotation.Autowired;
7   -import org.springframework.beans.factory.annotation.Value;
8 9 import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
9 10 import org.springframework.context.annotation.Bean;
10 11 import org.springframework.context.annotation.Configuration;
... ... @@ -21,9 +22,6 @@ import org.springframework.security.web.firewall.DefaultHttpFirewall;
21 22 import org.springframework.security.web.firewall.HttpFirewall;
22 23 import org.springframework.security.web.session.HttpSessionEventPublisher;
23 24  
24   -import com.bsth.common.Constants;
25   -import com.bsth.security.filter.LoginInterceptor;
26   -
27 25 @Configuration
28 26 @EnableWebSecurity
29 27 public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
... ... @@ -78,9 +76,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
78 76 .sessionRegistry(sessionRegistry());
79 77  
80 78 WhiteIpFilter whiteIpFilter = new WhiteIpFilter();
81   - whiteIpFilter.setSetting(setting);
82 79 http.addFilterBefore(whiteIpFilter, FilterSecurityInterceptor.class);
83 80 http.addFilterBefore(new LoginInterceptor(), FilterSecurityInterceptor.class);
  81 + http.addFilterBefore(new AuthorityFilter(), FilterSecurityInterceptor.class);
84 82 http.addFilter(filterSecurityInterceptor());
85 83 }
86 84  
... ...
src/main/java/com/bsth/service/sys/impl/ModuleServiceImpl.java
... ... @@ -74,7 +74,7 @@ public class ModuleServiceImpl extends BaseServiceImpl&lt;Module, Integer&gt; implemen
74 74 Map<Integer, Module> map = new HashMap<>();
75 75 for(Module m : all){
76 76 map.put(m.getId(), m);
77   - if(m.getGroupType().equals("3"))
  77 + if(m.getGroupType().equals("3") && m.isEnable())
78 78 rs.add(m);
79 79 }
80 80  
... ...
src/main/resources/application-cloud.properties
... ... @@ -63,6 +63,7 @@ cp.ack.url= https://58.247.254.118:4003/prod-api/serverApi/instructionsIssue/con
63 63 admin.mail= 3090342880@qq.com
64 64 ## enabled
65 65 enabled.whiteip= true
  66 +enabled.authority= false
66 67  
67 68 sso.enabled= true
68 69 sso.systemcode = SYS0023
... ...
src/main/resources/application-dev.properties
... ... @@ -64,7 +64,8 @@ cp.ack.url= http://114.80.178.12:8778/prod-api/serverApi/instructionsIssue/confi
64 64 ## admin mail
65 65 admin.mail= 3090342880@qq.com
66 66 ## enabled
67   -enabled.whiteip= true
  67 +enabled.whiteip= false
  68 +enabled.authority= false
68 69  
69 70 sso.enabled= false
70 71 sso.systemcode = SYS0019
... ...
src/main/resources/application-test.properties
... ... @@ -63,6 +63,7 @@ cp.ack.url= http://114.80.178.12:8778/prod-api/serverApi/instructionsIssue/confi
63 63 admin.mail= 3090342880@qq.com
64 64 ## enabled
65 65 enabled.whiteip= false
  66 +enabled.authority= false
66 67  
67 68 sso.enabled= true
68 69 sso.systemcode = SYS0023
... ...