通用模块开发

This commit is contained in:
haoxin963
2021-06-25 21:09:13 +08:00
parent 7628340be2
commit cec1b851a3
10 changed files with 88 additions and 12 deletions

View File

@@ -1,2 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xtoon.cloud.common.mybatis.MybatisConfiguration
com.xtoon.cloud.common.mybatis.MybatisConfiguration,\
com.xtoon.cloud.common.mybatis.util.MybatisInterceptor

View File

@@ -7,7 +7,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 类描述
* 拦截器配置
*
* @author haoxin
* @date 2021-06-24

View File

@@ -1,5 +1,6 @@
package com.xtoon.cloud.ops.auth;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@@ -12,6 +13,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
**/
@EnableDiscoveryClient
@SpringBootApplication
@DubboComponentScan(basePackages = "com.xtoon.cloud.ops.auth")
public class XtoonCloudAuthApplication {
public static void main(String[] args) {
SpringApplication.run(XtoonCloudAuthApplication.class);

View File

@@ -9,6 +9,7 @@ import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -22,7 +23,7 @@ public class LogoutController {
private RedisService redisService;
@DeleteMapping("/logout")
@PostMapping("/logout")
public Result logout() {
JSONObject jsonObject = RequestUtils.getJwtPayload();
String jti = jsonObject.getStr(AuthConstants.JWT_JTI);

View File

@@ -0,0 +1,62 @@
package com.xtoon.cloud.ops.gateway.config;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.cors.reactive.CorsUtils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
/**
* 路由配置信息
*
* @author Chill
*/
@Slf4j
@Configuration
@AllArgsConstructor
public class RouterFunctionConfiguration {
/**
* 这里为支持的请求头如果有自定义的header字段请自己添加
*/
private static final String ALLOWED_HEADERS = "X-Requested-With, tenant_id, Blade-Auth, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, username, client, knfie4j-gateway-request, request-origion";
private static final String ALLOWED_METHODS = "GET,POST,PUT,DELETE,OPTIONS,HEAD";
private static final String ALLOWED_ORIGIN = "http://localhost:8001";
private static final String ALLOWED_EXPOSE = "*";
private static final String MAX_AGE = "18000L";
/**
* 跨域配置
*/
@Bean
public WebFilter corsFilter() {
return (ServerWebExchange ctx, WebFilterChain chain) -> {
ServerHttpRequest request = ctx.getRequest();
if (CorsUtils.isCorsRequest(request)) {
ServerHttpResponse response = ctx.getResponse();
HttpHeaders headers = response.getHeaders();
headers.add("Access-Control-Allow-Headers", ALLOWED_HEADERS);
headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS);
headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN);
headers.add("Access-Control-Expose-Headers", ALLOWED_EXPOSE);
headers.add("Access-Control-Max-Age", MAX_AGE);
headers.add("Access-Control-Allow-Credentials", "true");
if (request.getMethod() == HttpMethod.OPTIONS) {
response.setStatusCode(HttpStatus.OK);
return Mono.empty();
}
}
return chain.filter(ctx);
};
}
}

View File

@@ -58,10 +58,8 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
}
// 存在token且不是黑名单request写入JWT的载体信息
// String tenantId = request.getHeaders().getFirst(CommonConstant.TENANT_ID);
request = exchange.getRequest().mutate()
.header(AuthConstants.JWT_PAYLOAD_KEY, payload)
// .header(CommonConstant.TENANT_ID, tenantId)
.build();
exchange = exchange.mutate().request(request).build();
return chain.filter(exchange);

View File

@@ -19,9 +19,21 @@ import java.nio.charset.StandardCharsets;
**/
public class WebUtils {
private static final String ALLOWED_HEADERS = "X-Requested-With, tenant_id, Blade-Auth, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, username, client, knfie4j-gateway-request, request-origion";
private static final String ALLOWED_METHODS = "GET,POST,PUT,DELETE,OPTIONS,HEAD";
private static final String ALLOWED_ORIGIN = "http://localhost:8001";
private static final String ALLOWED_EXPOSE = "*";
private static final String MAX_AGE = "18000L";
public static Mono<Void> getAuthFailResult(ServerHttpResponse response, Integer code) {
response.setStatusCode(HttpStatus.OK);
response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
response.getHeaders().add("Access-Control-Allow-Headers", ALLOWED_HEADERS);
response.getHeaders().add("Access-Control-Allow-Methods", ALLOWED_METHODS);
response.getHeaders().add("Access-Control-Allow-Origin", ALLOWED_ORIGIN);
response.getHeaders().add("Access-Control-Expose-Headers", ALLOWED_EXPOSE);
response.getHeaders().add("Access-Control-Max-Age", MAX_AGE);
response.getHeaders().add("Access-Control-Allow-Credentials", "true");
byte[] responseByte = new Gson().toJson(Result.error(code, ResultCode.getValue(code).getMsg())).getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = response.bufferFactory().wrap(responseByte);
return response.writeWith(Flux.just(buffer));

View File

@@ -1,5 +1,6 @@
package com.xtoon.cloud.sys;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@@ -12,6 +13,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
**/
@EnableDiscoveryClient
@SpringBootApplication
@DubboComponentScan(basePackages = "com.xtoon.cloud.sys")
public class XtoonCloudSysApplication {
public static void main(String[] args) {
SpringApplication.run(XtoonCloudSysApplication.class);

View File

@@ -23,7 +23,7 @@ import java.util.List;
* @author haoxin
* @date 2021-05-10
**/
@DubboService(timeout = 3000)
@DubboService
public class AuthenticationServiceImpl implements AuthenticationService {
@Autowired

View File

@@ -2,12 +2,10 @@
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<!-- 开发、测试环境 -->
<springProfile name="dev">
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="INFO" />
<logger name="com.xtoon" level="DEBUG" />
</springProfile>
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="INFO" />
<logger name="com.xtoon" level="DEBUG" />
<springProfile name="test,prod">
<appender name="STDOUT"