authList = new ArrayList<>();
+ for (Map map : maplist) {
+ String authority = (String) map.get("authority");
+ authList.add(new SimpleGrantedAuthority(authority));
+ }
+ return new UsernamePasswordAuthenticationToken(username, null, authList);
+ } else {
+ return new UsernamePasswordAuthenticationToken(username, null, new ArrayList<>());
+ }
+ }
+ }
+ return null;
+ }
+}
diff --git a/common/spring-security/src/main/java/com/atguigu/security/fillter/TokenAuthenticationFilter.java b/common/spring-security/src/main/java/com/atguigu/security/filter/TokenLoginFilter.java
similarity index 54%
rename from common/spring-security/src/main/java/com/atguigu/security/fillter/TokenAuthenticationFilter.java
rename to common/spring-security/src/main/java/com/atguigu/security/filter/TokenLoginFilter.java
index 7ab5990..61de791 100644
--- a/common/spring-security/src/main/java/com/atguigu/security/fillter/TokenAuthenticationFilter.java
+++ b/common/spring-security/src/main/java/com/atguigu/security/filter/TokenLoginFilter.java
@@ -1,5 +1,6 @@
-package com.atguigu.security.fillter;
+package com.atguigu.security.filter;
+import com.alibaba.fastjson.JSON;
import com.atguigu.common.result.Result;
import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.common.utlis.JwtHelper;
@@ -7,6 +8,7 @@ import com.atguigu.common.utlis.ResponseUtil;
import com.atguigu.security.custom.CustomUser;
import com.atguigu.vo.system.LoginVo;
import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
@@ -15,67 +17,68 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import javax.servlet.FilterChain;
-import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
-/**
- *
- * 登录过滤器,继承UsernamePasswordAuthenticationFilter,对用户名密码进行登录校验
- *
- */
-public class TokenAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
- public TokenAuthenticationFilter(AuthenticationManager authenticationManager) {
+public class TokenLoginFilter extends UsernamePasswordAuthenticationFilter {
+ private final RedisTemplate redisTemplate;
+
+ // 构造方法
+ public TokenLoginFilter(AuthenticationManager authenticationManager,
+ RedisTemplate redisTemplate) {
this.setAuthenticationManager(authenticationManager);
this.setPostOnly(false);
// 指定登录接口及提交方式,可以指定任意路径
this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/admin/system/index/login", "POST"));
+ this.redisTemplate = redisTemplate;
}
- /**
- * 登录认证
- */
- @Override
- public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res)
+ // 登录认证
+ // 获取输入的用户名和密码,调用方法认证
+ public Authentication attemptAuthentication(HttpServletRequest request,
+ HttpServletResponse response)
throws AuthenticationException {
try {
- LoginVo loginVo = new ObjectMapper().readValue(req.getInputStream(), LoginVo.class);
-
- Authentication authenticationToken = new UsernamePasswordAuthenticationToken(loginVo.getUsername(), loginVo.getPassword());
+ // 获取用户信息
+ LoginVo loginVo = new ObjectMapper().readValue(request.getInputStream(), LoginVo.class);
+ // 封装对象
+ Authentication authenticationToken =
+ new UsernamePasswordAuthenticationToken(loginVo.getUsername(), loginVo.getPassword());
+ // 调用方法
return this.getAuthenticationManager().authenticate(authenticationToken);
} catch (IOException e) {
throw new RuntimeException(e);
}
-
}
- /**
- * 登录成功
- */
- @Override
- protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
- Authentication auth) throws IOException, ServletException {
+ // 认证成功调用方法
+ protected void successfulAuthentication(HttpServletRequest request,
+ HttpServletResponse response,
+ FilterChain chain,
+ Authentication auth) {
+ // 获取当前用户
CustomUser customUser = (CustomUser) auth.getPrincipal();
- String token = JwtHelper.createToken(customUser.getSysUser().getId(), customUser.getSysUser().getUsername());
+ // 生成token
+ String token = JwtHelper.createToken(customUser.getSysUser().getId(),
+ customUser.getSysUser().getUsername());
+ // 获取当前用户权限数据,放到Redis里面 key:username value:权限数据
+ redisTemplate.opsForValue().set(customUser.getUsername(),
+ JSON.toJSONString(customUser.getAuthorities()));
+
+ // 返回
Map map = new HashMap<>();
map.put("token", token);
ResponseUtil.out(response, Result.success(map));
}
- /**
- * 登录失败
- */
- @Override
- protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
- AuthenticationException e) {
- if (e.getCause() instanceof RuntimeException) {
- ResponseUtil.out(response, Result.error(null, 204, e.getMessage()));
- } else {
- ResponseUtil.out(response, Result.error(null, ResultCodeEnum.LOGIN_MOBLE_ERROR));
- }
+ // 认证失败调用方法
+ protected void unsuccessfulAuthentication(HttpServletRequest request,
+ HttpServletResponse response,
+ AuthenticationException failed) {
+ ResponseUtil.out(response, Result.error(null, ResultCodeEnum.LOGIN_MOBLE_ERROR));
}
-}
\ No newline at end of file
+}
diff --git a/logs/service-oa/spring.log b/logs/service-oa/spring.log
index 761fdfe..1566d36 100644
--- a/logs/service-oa/spring.log
+++ b/logs/service-oa/spring.log
@@ -405,3 +405,38 @@ Using generated security password: c1b9b421-40ec-420f-88cd-c249c5d26684
14:54:25:578 INFO 16348 --- [http-nio-8800-exec-4] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
14:54:25:583 INFO 16348 --- [http-nio-8800-exec-4] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
16:12:27:427 INFO 16348 --- [SpringContextShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
+16:22:14:238 INFO 23040 --- [main] com.atguigu.auth.ServiceAuthApplication : Starting ServiceAuthApplication on Bunny with PID 23040 (F:\java项目\guigu-oa\guigu-oa\service-oa\target\classes started by ACE in F:\java项目\guigu-oa\guigu-oa)
+16:22:14:239 INFO 23040 --- [main] com.atguigu.auth.ServiceAuthApplication : The following profiles are active: dev
+16:22:14:830 INFO 23040 --- [main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
+16:22:14:833 INFO 23040 --- [main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
+16:22:14:855 INFO 23040 --- [main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 13ms. Found 0 Redis repository interfaces.
+16:22:15:081 INFO 23040 --- [main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@3c232051' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
+16:22:15:085 INFO 23040 --- [main] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
+16:22:15:412 INFO 23040 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8800 (http)
+16:22:15:418 INFO 23040 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
+16:22:15:418 INFO 23040 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39]
+16:22:15:478 INFO 23040 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
+16:22:15:478 INFO 23040 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1206 ms
+16:22:15:587 INFO 23040 --- [main] com.atguigu.config.MybatisPlusConfig : 注入MybatisPlus配置类...
+16:22:16:085 INFO 23040 --- [main] com.atguigu.config.RedisConfiguration : RedisConfiguration===>使用StringRedisSerializer序列化为字符串
+16:22:16:347 INFO 23040 --- [main] com.atguigu.config.RedisConfiguration : RedisConfiguration===>解决cache(@Cacheable)把数据缓存到redis中的value是乱码问题
+16:22:16:351 INFO 23040 --- [main] com.atguigu.config.RedisConfiguration : RedisConfiguration===>指定的日期模式
+16:22:16:428 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/admin/modeler/**'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/diagram-viewer/**'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/editor-app/**'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/*.html'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/admin/processImage/**'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/admin/wechat/authorize'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/admin/wechat/userInfo'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/admin/wechat/bindPhone'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/favicon.ico'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/swagger-resources/**'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/webjars/**'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/v2/**'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/swagger-ui.html/**'], []
+16:22:16:429 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/doc.html'], []
+16:22:16:495 INFO 23040 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@39403943, org.springframework.security.web.context.SecurityContextPersistenceFilter@6e7f29d5, org.springframework.security.web.header.HeaderWriterFilter@ff8e36d, org.springframework.web.filter.CorsFilter@1c5fd813, org.springframework.security.web.authentication.logout.LogoutFilter@63ad5fe7, com.atguigu.security.filter.TokenAuthenticationFilter@49338f3, com.atguigu.security.filter.TokenLoginFilter@3c9971af, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@7f94541b, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@618fb1, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4abdd5e, org.springframework.security.web.session.SessionManagementFilter@4f22fd5d, org.springframework.security.web.access.ExceptionTranslationFilter@94aeba1, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@7adbec34]
+16:22:16:531 INFO 23040 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
+16:22:16:712 INFO 23040 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8800 (http) with context path ''
+16:22:16:982 INFO 23040 --- [main] com.atguigu.auth.ServiceAuthApplication : Started ServiceAuthApplication in 3.025 seconds (JVM running for 3.551)
+16:22:35:146 INFO 23040 --- [SpringContextShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'