feat: guava缓存/登录拦截器

This commit is contained in:
2024-03-04 17:39:22 +08:00
parent c0ec4e34f8
commit 9f36b9a126
30 changed files with 503 additions and 98 deletions

View File

@@ -19,10 +19,19 @@
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
</dependencies>
</project>

View File

@@ -1,13 +0,0 @@
package com.landaiqing;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

@@ -0,0 +1,22 @@
package com.landaiqing.auth.api;
import com.landaiqing.auth.entity.AuthUserDTO;
import com.landaiqing.auth.entity.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Classname UserFeignService
* @BelongsProject: qing-yu-club
* @BelongsPackage: com.landaiqing.auth.api
* @Author: landaiqing
* @CreateTime: 2024-03-03 19:57
* @Description: 用户服务feign
* @Version: 1.0
*/
@FeignClient("qing-yu-club-auth-dev")
public interface UserFeignService {
@RequestMapping("/user/getUserInfo")
Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO);
}

View File

@@ -1,6 +1,5 @@
package com.landaiqing.auth.common.entity;
package com.landaiqing.auth.entity;
import com.landaiqing.auth.common.enums.ResultCodeEnum;
import lombok.Data;
@Data

View File

@@ -0,0 +1,29 @@
package com.landaiqing.auth.entity;
import lombok.Getter;
@Getter
public enum ResultCodeEnum {
SUCCESS(200,"成功"),
FAIL(500,"失败");
public int code;
public String desc;
ResultCodeEnum(int code,String desc){
this.code = code;
this.desc = desc;
}
public static ResultCodeEnum getByCode(int codeVal){
for(ResultCodeEnum resultCodeEnum : ResultCodeEnum.values()){
if(resultCodeEnum.code == codeVal){
return resultCodeEnum;
}
}
return null;
}
}

View File

@@ -1,38 +0,0 @@
package com.landaiqing;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

View File

@@ -32,6 +32,12 @@
<artifactId>qing-yu-club-auth-domain</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.landaiqing</groupId>
<artifactId>qing-yu-club-auth-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@@ -3,9 +3,11 @@ package com.landaiqing.auth.application.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.landaiqing.auth.application.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.List;
@@ -33,4 +35,9 @@ public class GlobalConfig extends WebMvcConfigurationSupport {
MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter(objectMapper);
return converter;
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**");
}
}

View File

@@ -0,0 +1,42 @@
package com.landaiqing.auth.application.context;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Classname LoginContextHolder
* @BelongsProject: qing-yu-club
* @BelongsPackage: com.landaiqing.subject.application.context
* @Author: landaiqing
* @CreateTime: 2024-03-03 18:11
* @Description: 登录上下文对象
* @Version: 1.0
*/
public class LoginContextHolder {
private static final InheritableThreadLocal <Map<String,Object>> THREAD_LOCAL=new InheritableThreadLocal<>();
public static void set(String key,Object val){
Map<String,Object> map=getThreadLoacalMap();
map.put(key, val);
}
public static Object get(String key){
Map<String,Object> threadLoacalMap=getThreadLoacalMap();
return threadLoacalMap.get(key);
}
public static void remove(){
THREAD_LOCAL.remove();
}
public static String getLoginId(){
return (String) getThreadLoacalMap().get("loginId");
}
public static Map<String,Object> getThreadLoacalMap(){
Map<String,Object> map =THREAD_LOCAL.get();
if (Objects.isNull(map)){
map=new ConcurrentHashMap<>();
THREAD_LOCAL.set(map);
}
return map;
}
}

View File

@@ -6,7 +6,7 @@ import com.landaiqing.auth.application.convert.AuthPermissionDTOConverter;
import com.landaiqing.auth.application.dto.AuthPermissionDTO;
import com.landaiqing.auth.domain.entity.AuthPermissionBO;
import com.landaiqing.auth.domain.service.AuthPermissionDomainService;
import com.landaiqing.auth.common.entity.Result;
import com.landaiqing.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

View File

@@ -4,9 +4,9 @@ import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.landaiqing.auth.application.convert.AuthRoleDTOConverter;
import com.landaiqing.auth.application.dto.AuthRoleDTO;
import com.landaiqing.auth.common.entity.Result;
import com.landaiqing.auth.domain.entity.AuthRoleBO;
import com.landaiqing.auth.domain.service.AuthRoleDomainService;
import com.landaiqing.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

View File

@@ -4,9 +4,9 @@ import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.landaiqing.auth.application.convert.AuthRolePermissionDTOConverter;
import com.landaiqing.auth.application.dto.AuthRolePermissionDTO;
import com.landaiqing.auth.common.entity.Result;
import com.landaiqing.auth.domain.entity.AuthRolePermissionBO;
import com.landaiqing.auth.domain.service.AuthRolePermissionDomainService;
import com.landaiqing.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

View File

@@ -5,10 +5,10 @@ import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.landaiqing.auth.application.convert.AuthUserDTOConverter;
import com.landaiqing.auth.application.dto.AuthUserDTO;
import com.landaiqing.auth.common.entity.Result;
import com.landaiqing.auth.domain.entity.AuthUserBO;
import com.landaiqing.auth.domain.service.AuthUserDomainService;
import com.landaiqing.auth.entity.AuthUserDTO;
import com.landaiqing.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

View File

@@ -1,7 +1,7 @@
package com.landaiqing.auth.application.convert;
import com.landaiqing.auth.application.dto.AuthUserDTO;
import com.landaiqing.auth.domain.entity.AuthUserBO;
import com.landaiqing.auth.entity.AuthUserDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

View File

@@ -0,0 +1,31 @@
package com.landaiqing.auth.application.interceptor;
import com.landaiqing.auth.application.context.LoginContextHolder;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Classname LoginInterceptor
* @BelongsProject: qing-yu-club
* @BelongsPackage: com.landaiqing.subject.application.interceptor
* @Author: landaiqing
* @CreateTime: 2024-03-03 18:05
* @Description: 登录拦截器
* @Version: 1.0
*/
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String loginId = request.getHeader("loginId");
LoginContextHolder.set("loginId",loginId);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
LoginContextHolder.remove();
}
}

View File

@@ -24,7 +24,7 @@
select id,
name,
parent_id,
type,
`type`,
menu_url,
status, `show`, icon, permission_key, created_by, created_time, update_by, update_time, is_deleted
from auth_permission
@@ -34,7 +34,7 @@
<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="AuthPermissionMap">
select
id, name, parent_id, type, menu_url, status, `show`, icon, permission_key, created_by, created_time, update_by,
id, name, parent_id, `type`, menu_url, status, `show`, icon, permission_key, created_by, created_time, update_by,
update_time, is_deleted
from auth_permission
<where>
@@ -42,22 +42,22 @@
and id = #{id}
</if>
<if test="name != null and name != ''">
and name = #{name}
and `name` = #{name}
</if>
<if test="parentId != null">
and parent_id = #{parentId}
</if>
<if test="type != null">
and type = #{type}
and `type` = #{type}
</if>
<if test="menuUrl != null and menuUrl != ''">
and menu_url = #{menuUrl}
</if>
<if test="status != null">
and status = #{status}
and `status` = #{status}
</if>
<if test="show != null">
and show = #{show}
and `show` = #{show}
</if>
<if test="icon != null and icon != ''">
and icon = #{icon}
@@ -92,7 +92,7 @@
and id = #{id}
</if>
<if test="name != null and name != ''">
and name = #{name}
and `name` = #{name}
</if>
<if test="parentId != null">
and parent_id = #{parentId}
@@ -104,10 +104,10 @@
and menu_url = #{menuUrl}
</if>
<if test="status != null">
and status = #{status}
and `status` = #{status}
</if>
<if test="show != null">
and show = #{show}
and `show` = #{show}
</if>
<if test="icon != null and icon != ''">
and icon = #{icon}