Browse Source

feat 缓存模块增加

1 month ago
parent
commit
dba645d758

+ 17 - 3
edu-travel-oauth/src/main/java/edu/travel/filter/TokenAuthenticationFilter.java

@@ -6,6 +6,8 @@ import com.alibaba.fastjson.JSONObject;
 import edu.travel.EncryptUtil;
 import edu.travel.entity.EduTenant;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.authority.AuthorityUtils;
 import org.springframework.security.core.context.SecurityContextHolder;
@@ -21,6 +23,8 @@ import java.io.IOException;
 
 @Component
 public class TokenAuthenticationFilter extends OncePerRequestFilter {
+    @Autowired
+    private RedisTemplate<String, Object> redisTemplate;
     @Override
     protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
         String token = httpServletRequest.getHeader("token");
@@ -30,13 +34,23 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
             JSONObject jsonObject = JSON.parseObject(json);
             //用户身份信息
             String username  = jsonObject.getString("principal");
-            EduTenant user = new EduTenant();
-            user.setTenantPhone(username);
+            Object object = redisTemplate.opsForValue().get(username + "_info");
+            if (object == null){
+                JSONObject resultObject = new JSONObject();
+                resultObject.put("code",401);
+                resultObject.put("msg","not found user");
+                resultObject.put("data",null);
+                httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+                httpServletResponse.setContentType("application/json;charset=utf-8");
+                httpServletResponse.getWriter().write(resultObject.toJSONString());
+                return;
+            }
+            EduTenant eduTenant = JSON.parseObject(object.toString(), EduTenant.class);
             //用户权限
             JSONArray authoritiesArray = jsonObject.getJSONArray("authorities");
             String[] authorities = authoritiesArray.toArray(new String[authoritiesArray.size()]);
             //将用户信息和权限填充 到用户身份token对象中
-            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, null, AuthorityUtils.createAuthorityList(authorities));
+            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(eduTenant, null, AuthorityUtils.createAuthorityList(authorities));
             authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
             //将authenticationToken填充到安全上下文
             SecurityContextHolder.getContext().setAuthentication(authenticationToken);

+ 2 - 0
edu-travel-oauth/src/main/java/edu/travel/service/UserServiceImpl.java

@@ -1,5 +1,6 @@
 package edu.travel.service;
 
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import edu.travel.entity.EduTenant;
 import edu.travel.mapper.EduTenantMapper;
@@ -42,6 +43,7 @@ public class UserServiceImpl implements UserDetailsService {
             throw new UsernameNotFoundException("验证码为空");
         }
         if (code.equals(object.toString())){
+            redisTemplate.opsForValue().set(username+"_info", JSON.toJSONString(tenant));
             return new User(tenant.getTenantPhone(),code,new ArrayList<>());
         }
         throw new UsernameNotFoundException("验证码错误");

+ 5 - 0
edu-travel-service/edu-travel-service-tenement/pom.xml

@@ -120,6 +120,11 @@
             <artifactId>edu-travel-common-core</artifactId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>edu.travel</groupId>
+            <artifactId>edu-travel-common-cache</artifactId>
+            <version>1.0-SNAPSHOT</version>
+        </dependency>
     </dependencies>
     <profiles>
         <profile>

+ 17 - 3
edu-travel-service/edu-travel-service-tenement/src/main/java/edu/travel/tenant/filter/TokenAuthenticationFilter.java

@@ -6,6 +6,8 @@ import com.alibaba.fastjson.JSONObject;
 import edu.travel.EncryptUtil;
 import edu.travel.tenant.entity.EduTenant;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.authority.AuthorityUtils;
 import org.springframework.security.core.context.SecurityContextHolder;
@@ -21,6 +23,8 @@ import java.io.IOException;
 
 @Component
 public class TokenAuthenticationFilter extends OncePerRequestFilter {
+    @Autowired
+    private RedisTemplate<String, Object> redisTemplate;
     @Override
     protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
         String token = httpServletRequest.getHeader("token");
@@ -30,13 +34,23 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
             JSONObject jsonObject = JSON.parseObject(json);
             //用户身份信息
             String username  = jsonObject.getString("principal");
-            EduTenant tenant = new EduTenant();
-            tenant.setTenantPhone(username);
+            Object object = redisTemplate.opsForValue().get(username + "_info");
+            if (object == null){
+                JSONObject resultObject = new JSONObject();
+                resultObject.put("code",401);
+                resultObject.put("msg","not found user");
+                resultObject.put("data",null);
+                httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+                httpServletResponse.setContentType("application/json;charset=utf-8");
+                httpServletResponse.getWriter().write(resultObject.toJSONString());
+                return;
+            }
+            EduTenant eduTenant = JSON.parseObject(object.toString(), EduTenant.class);
             //用户权限
             JSONArray authoritiesArray = jsonObject.getJSONArray("authorities");
             String[] authorities = authoritiesArray.toArray(new String[authoritiesArray.size()]);
             //将用户信息和权限填充 到用户身份token对象中
-            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(tenant, null, AuthorityUtils.createAuthorityList(authorities));
+            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(eduTenant, null, AuthorityUtils.createAuthorityList(authorities));
             authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
             //将authenticationToken填充到安全上下文
             SecurityContextHolder.getContext().setAuthentication(authenticationToken);