|
|
@@ -1,6 +1,7 @@
|
|
|
package com.uas.platform.b2b.filter;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
|
import javax.servlet.Filter;
|
|
|
import javax.servlet.FilterChain;
|
|
|
@@ -10,13 +11,26 @@ import javax.servlet.ServletRequest;
|
|
|
import javax.servlet.ServletResponse;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.security.access.SecurityMetadataSource;
|
|
|
import org.springframework.security.access.intercept.AbstractSecurityInterceptor;
|
|
|
import org.springframework.security.access.intercept.InterceptorStatusToken;
|
|
|
+import org.springframework.security.authentication.AuthenticationManager;
|
|
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.AuthenticationException;
|
|
|
+import org.springframework.security.core.GrantedAuthority;
|
|
|
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
import org.springframework.security.web.FilterInvocation;
|
|
|
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
|
|
|
+import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
import com.uas.platform.b2b.model.User;
|
|
|
+import com.uas.platform.b2b.service.UserService;
|
|
|
import com.uas.platform.b2b.support.SystemSession;
|
|
|
|
|
|
/**
|
|
|
@@ -29,6 +43,13 @@ public class SecurityInterceptor extends AbstractSecurityInterceptor implements
|
|
|
|
|
|
private FilterInvocationSecurityMetadataSource securityMetadataSource;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ @Qualifier("org.springframework.security.authenticationManager")
|
|
|
+ protected AuthenticationManager authenticationManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
/**
|
|
|
* @param request
|
|
|
* @param response
|
|
|
@@ -36,8 +57,8 @@ public class SecurityInterceptor extends AbstractSecurityInterceptor implements
|
|
|
* @throws IOException
|
|
|
* @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);
|
|
|
FilterInvocation fi = new FilterInvocation(request, response, chain);
|
|
|
invoke(fi);
|
|
|
@@ -95,4 +116,42 @@ public class SecurityInterceptor extends AbstractSecurityInterceptor implements
|
|
|
SystemSession.setUser((User) user);
|
|
|
}
|
|
|
|
|
|
+ static final String TEL_REGEXP = "^((\\(\\d{3}\\))|(\\d{3}\\-))?(13|15|18)\\d{9}$";
|
|
|
+
|
|
|
+ static final String UU_REGEXP = "^\\d{4,}$";
|
|
|
+
|
|
|
+ static final String ROLE_USER = "ROLE_USER";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自动登录
|
|
|
+ */
|
|
|
+ private void autoLogin(HttpServletRequest request) {
|
|
|
+ String username = request.getParameter("b_username");
|
|
|
+ String password = request.getParameter("b_password");
|
|
|
+ if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
|
|
|
+ User user = null;
|
|
|
+ if (username.contains("@")) { // 邮箱登录
|
|
|
+ user = userService.findUserByUserEmail(username);
|
|
|
+ } else if (username.matches(TEL_REGEXP)) {// 手机号登录
|
|
|
+ user = userService.findUserByUserTel(username);
|
|
|
+ } else if (username.matches(UU_REGEXP)) {
|
|
|
+ user = userService.findUserByUserUU(Long.parseLong(username));
|
|
|
+ }
|
|
|
+ if (user != null) {
|
|
|
+ ArrayList<GrantedAuthority> array = new ArrayList<GrantedAuthority>();
|
|
|
+ array.add(new SimpleGrantedAuthority(ROLE_USER));
|
|
|
+ UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUserUU(), password, array);
|
|
|
+ try {
|
|
|
+ Authentication authenticatedUser = authenticationManager.authenticate(token);
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
|
|
|
+ request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
|
|
|
+ SecurityContextHolder.getContext());
|
|
|
+ } catch (AuthenticationException e) {
|
|
|
+ throw new UsernameNotFoundException("密码错误");
|
|
|
+ }
|
|
|
+ } else
|
|
|
+ throw new UsernameNotFoundException(username + "账号不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|