|
@@ -1,7 +1,9 @@
|
|
|
package com.uas.platform.b2b.filter;
|
|
package com.uas.platform.b2b.filter;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.HashSet;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
|
|
import javax.servlet.Filter;
|
|
import javax.servlet.Filter;
|
|
|
import javax.servlet.FilterChain;
|
|
import javax.servlet.FilterChain;
|
|
@@ -10,6 +12,7 @@ import javax.servlet.ServletException;
|
|
|
import javax.servlet.ServletRequest;
|
|
import javax.servlet.ServletRequest;
|
|
|
import javax.servlet.ServletResponse;
|
|
import javax.servlet.ServletResponse;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
@@ -26,11 +29,17 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
import org.springframework.security.web.FilterInvocation;
|
|
import org.springframework.security.web.FilterInvocation;
|
|
|
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
|
|
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
|
|
|
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
|
|
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
|
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
+import com.uas.platform.b2b.model.Authority;
|
|
|
|
|
+import com.uas.platform.b2b.model.Resource;
|
|
|
|
|
+import com.uas.platform.b2b.model.Role;
|
|
|
import com.uas.platform.b2b.model.User;
|
|
import com.uas.platform.b2b.model.User;
|
|
|
import com.uas.platform.b2b.service.UserService;
|
|
import com.uas.platform.b2b.service.UserService;
|
|
|
|
|
+import com.uas.platform.b2b.support.SecurityConstant;
|
|
|
import com.uas.platform.b2b.support.SystemSession;
|
|
import com.uas.platform.b2b.support.SystemSession;
|
|
|
|
|
+import com.uas.platform.core.model.Constant;
|
|
|
import com.uas.platform.core.util.encry.Md5Utils;
|
|
import com.uas.platform.core.util.encry.Md5Utils;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -58,18 +67,28 @@ public class SecurityInterceptor extends AbstractSecurityInterceptor implements
|
|
|
* @throws ServletException
|
|
* @throws ServletException
|
|
|
*/
|
|
*/
|
|
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
|
|
- autoLogin((HttpServletRequest) request);
|
|
|
|
|
- logSession((HttpServletRequest) request);
|
|
|
|
|
|
|
+ HttpServletRequest httpRequest = (HttpServletRequest) request;
|
|
|
|
|
+ HttpServletResponse httpResponse = (HttpServletResponse) response;
|
|
|
|
|
+ autoLogin(httpRequest);
|
|
|
|
|
+ logSession(httpRequest);
|
|
|
|
|
+ User user = SystemSession.getUser();
|
|
|
|
|
+ if (user == null) {// 未登录则要求登录
|
|
|
|
|
+ httpResponse.sendRedirect(httpRequest.getContextPath() + SecurityConstant.LOGIN_URL);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (user.isSys()) {// 超级用户无需验证权限
|
|
|
|
|
+ chain.doFilter(request, response);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
invoke(new FilterInvocation(request, response, chain));
|
|
invoke(new FilterInvocation(request, response, chain));
|
|
|
logoutSession();
|
|
logoutSession();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void invoke(FilterInvocation fi) {
|
|
|
|
|
- InterceptorStatusToken token = super.beforeInvocation(fi);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ public void invoke(FilterInvocation filter) throws IOException, ServletException {
|
|
|
|
|
+ InterceptorStatusToken token = super.beforeInvocation(filter);
|
|
|
try {
|
|
try {
|
|
|
- fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
|
|
+ filter.getChain().doFilter(filter.getRequest(), filter.getResponse());
|
|
|
|
|
+ } finally {
|
|
|
super.afterInvocation(token, null);
|
|
super.afterInvocation(token, null);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -123,7 +142,7 @@ public class SecurityInterceptor extends AbstractSecurityInterceptor implements
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 线程池策略下,不会频繁删除线程,置于线程内的对象须手动删除
|
|
* 线程池策略下,不会频繁删除线程,置于线程内的对象须手动删除
|
|
|
*/
|
|
*/
|
|
@@ -153,8 +172,7 @@ public class SecurityInterceptor extends AbstractSecurityInterceptor implements
|
|
|
user = userService.findUserByUserUU(Long.parseLong(username));
|
|
user = userService.findUserByUserUU(Long.parseLong(username));
|
|
|
}
|
|
}
|
|
|
if (user != null && user.getUserPwd().equals(Md5Utils.encode(password, user.getUserUU()))) {
|
|
if (user != null && user.getUserPwd().equals(Md5Utils.encode(password, user.getUserUU()))) {
|
|
|
- ArrayList<GrantedAuthority> array = new ArrayList<GrantedAuthority>();
|
|
|
|
|
- array.add(new SimpleGrantedAuthority(ROLE_USER));
|
|
|
|
|
|
|
+ Collection<GrantedAuthority> array = getGrantedAuthorities(user);
|
|
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUserUU(), password, array);
|
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUserUU(), password, array);
|
|
|
Authentication authenticatedUser = authenticationManager.authenticate(token);
|
|
Authentication authenticatedUser = authenticationManager.authenticate(token);
|
|
|
SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
|
|
SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
|
|
@@ -167,4 +185,29 @@ public class SecurityInterceptor extends AbstractSecurityInterceptor implements
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private Set<GrantedAuthority> getGrantedAuthorities(User user) {
|
|
|
|
|
+ Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
|
|
|
|
|
+ Set<Role> roles = user.getRoles();
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(roles)) {
|
|
|
|
|
+ for (Role role : roles) {
|
|
|
|
|
+ if (role.isSys()) {// 超级账号
|
|
|
|
|
+ user.setIssys(Constant.YES);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ Set<Authority> authorities = role.getAuthorities();
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(authorities)) {
|
|
|
|
|
+ for (Authority authority : authorities) {
|
|
|
|
|
+ Set<Resource> resources = authority.getResources();
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(resources)) {
|
|
|
|
|
+ for (Resource res : resources) {
|
|
|
|
|
+ authSet.add(new SimpleGrantedAuthority(res.getName()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return authSet;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|