From bf24b36c62b32722e90411e1a129572dbeb911da Mon Sep 17 00:00:00 2001 From: zhuyijun Date: Mon, 10 Oct 2022 20:11:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EResponseResult=E4=B8=AD?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E8=BF=94=E5=9B=9E=E4=BD=93=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/zyjblogs/filter/CORSConfig.java | 29 +++++++++++ .../config/security/JwtTokenConfig.java | 2 +- .../security/OauthAuthenticationProvider.java | 13 ++--- .../server/user/vo/OAuth2AccessTokenVo.java | 13 +++-- .../mapper/mysql/user/UserMapper.xml | 14 ++++++ .../mapper/postgresql/user/UserMapper.xml | 14 ++++++ .../entity/response/ResponseResult.java | 10 +++- .../pom.xml | 48 +++++++++++++++++++ 8 files changed, 131 insertions(+), 12 deletions(-) create mode 100644 server/zyjblogs-gateway/src/main/java/cn/zyjblogs/filter/CORSConfig.java create mode 100644 server/zyjblogs-oauth/src/main/resources/mapper/mysql/user/UserMapper.xml create mode 100644 server/zyjblogs-oauth/src/main/resources/mapper/postgresql/user/UserMapper.xml create mode 100644 stater/zyjblogs-rabbitmq-spring-boot-starter/pom.xml diff --git a/server/zyjblogs-gateway/src/main/java/cn/zyjblogs/filter/CORSConfig.java b/server/zyjblogs-gateway/src/main/java/cn/zyjblogs/filter/CORSConfig.java new file mode 100644 index 0000000..0bc8219 --- /dev/null +++ b/server/zyjblogs-gateway/src/main/java/cn/zyjblogs/filter/CORSConfig.java @@ -0,0 +1,29 @@ +package cn.zyjblogs.filter; + +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.reactive.CorsWebFilter; +import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; +import org.springframework.web.util.pattern.PathPatternParser; + +@Component +public class CORSConfig { + @Bean + public CorsWebFilter corsWebFilter() { + + /** + * 返回一个CorsWebFilter ,构造其中需要传入连个形参,均为接口,可以直接new 接口 + * 是借口可以使用它的实现类来处理 + */ + CorsConfiguration config = new CorsConfiguration(); + config.addAllowedMethod("*"); + config.addAllowedOrigin("*"); + config.addAllowedHeader("*"); + config.setAllowCredentials(Boolean.TRUE); + config.addExposedHeader("Authorization"); + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser()); + source.registerCorsConfiguration("/**", config); + return new CorsWebFilter(source); + } +} diff --git a/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/config/security/JwtTokenConfig.java b/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/config/security/JwtTokenConfig.java index 5c06efe..dc3c6da 100644 --- a/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/config/security/JwtTokenConfig.java +++ b/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/config/security/JwtTokenConfig.java @@ -1,6 +1,6 @@ package cn.zyjblogs.config.security; -import cn.zyjblogs.config.rsa.RsaKeyProperties; +import cn.zyjblogs.starter.common.autoconfigure.rsa.RsaKeyProperties; import cn.zyjblogs.starter.common.entity.constant.CommonRedisKeyConstant; import cn.zyjblogs.starter.common.entity.response.HttpCode; import cn.zyjblogs.starter.common.exception.AuthRuntimeException; diff --git a/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/config/security/OauthAuthenticationProvider.java b/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/config/security/OauthAuthenticationProvider.java index b392b70..c87ba83 100644 --- a/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/config/security/OauthAuthenticationProvider.java +++ b/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/config/security/OauthAuthenticationProvider.java @@ -25,9 +25,10 @@ import java.util.Collection; public class OauthAuthenticationProvider extends DaoAuthenticationProvider { private UserDetailsService userDetailsService; private PasswordEncoder passwordEncoder; + public OauthAuthenticationProvider(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder - ){ + ) { this.passwordEncoder = passwordEncoder; this.userDetailsService = userDetailsService; this.setPasswordEncoder(passwordEncoder); @@ -37,22 +38,22 @@ public class OauthAuthenticationProvider extends DaoAuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = authentication.getName(); - if (authentication.getCredentials() == null){ + if (authentication.getCredentials() == null) { this.logger.debug("密码为空"); throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")); } String password = (String) authentication.getCredentials(); //获取用户信息 UserDetails user = userDetailsService.loadUserByUsername(username); - if (user == null){ + if (user == null) { this.logger.debug("用户不存在"); - throw new AuthRuntimeException(HttpCode.UNAUTHORIZED,"用户不存在"); + throw new AuthRuntimeException(HttpCode.UNAUTHORIZED, "用户不存在"); } OauthUserDetails userDetails = (OauthUserDetails) user; //比较前端传入的密码明文和数据库中加密的密码是否相等 if (!passwordEncoder.matches(password, userDetails.getPassword())) { this.logger.debug("密码不正确"); - throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")); + throw new AuthRuntimeException(HttpCode.BAD_REQUEST, "密码不正确"); } //获取用户权限信息 Collection authorities = userDetails.getAuthorities(); @@ -61,8 +62,8 @@ public class OauthAuthenticationProvider extends DaoAuthenticationProvider { } /** - * * 认证 + * * @param userDetails * @param authentication * @throws AuthenticationException diff --git a/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/server/user/vo/OAuth2AccessTokenVo.java b/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/server/user/vo/OAuth2AccessTokenVo.java index b661596..d523d04 100644 --- a/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/server/user/vo/OAuth2AccessTokenVo.java +++ b/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/server/user/vo/OAuth2AccessTokenVo.java @@ -9,6 +9,7 @@ import org.springframework.security.oauth2.common.OAuth2AccessToken; import java.io.Serializable; import java.util.Collection; +import java.util.Date; import java.util.Map; import java.util.Set; @@ -19,7 +20,7 @@ import java.util.Set; public class OAuth2AccessTokenVo implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "access token", dataType = "String", example = "abc.efg.hjk") - private String value; + private String token; @ApiModelProperty(value = "token类型", dataType = "String", example = "bearer") private String token_type; @@ -41,10 +42,12 @@ public class OAuth2AccessTokenVo implements Serializable { @ApiModelProperty(value = "用户账号", dataType = "String", example = "hangman") private String username; + @ApiModelProperty(value = "过期时间", dataType = "String", example = "hangman") + private Date expiration; - public OAuth2AccessTokenVo(String value, String tokenType, String refreshToken, int expiresIn, Set scope, Map addition) { - this.value = value; + public OAuth2AccessTokenVo(String value, String tokenType, String refreshToken, int expiresIn, Date expiration, Set scope, Map addition) { + this.token = value; this.token_type = tokenType; this.refresh_token = refreshToken; this.expires_in = expiresIn; @@ -52,6 +55,7 @@ public class OAuth2AccessTokenVo implements Serializable { this.userId = (String) addition.get(ContextKeyConstant.USER_ID_KEY); this.username = (String) addition.get(ContextKeyConstant.USERNAME_KEY); this.jti = (String) addition.get("jti"); + this.expiration = expiration; } public static OAuth2AccessTokenVo TransferToken(OAuth2AccessToken token) { @@ -60,12 +64,13 @@ public class OAuth2AccessTokenVo implements Serializable { token.getTokenType(), token.getRefreshToken().getValue(), token.getExpiresIn(), + token.getExpiration(), token.getScope(), token.getAdditionalInformation()); BaseContext.set(ContextDto.builder() .userId(oAuth2AccessTokenVo.getUserId()) .username(oAuth2AccessTokenVo.getUsername()) - .token(oAuth2AccessTokenVo.getValue()) + .token(oAuth2AccessTokenVo.getToken()) .build()); return oAuth2AccessTokenVo; } diff --git a/server/zyjblogs-oauth/src/main/resources/mapper/mysql/user/UserMapper.xml b/server/zyjblogs-oauth/src/main/resources/mapper/mysql/user/UserMapper.xml new file mode 100644 index 0000000..cb79ec8 --- /dev/null +++ b/server/zyjblogs-oauth/src/main/resources/mapper/mysql/user/UserMapper.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/server/zyjblogs-oauth/src/main/resources/mapper/postgresql/user/UserMapper.xml b/server/zyjblogs-oauth/src/main/resources/mapper/postgresql/user/UserMapper.xml new file mode 100644 index 0000000..cb79ec8 --- /dev/null +++ b/server/zyjblogs-oauth/src/main/resources/mapper/postgresql/user/UserMapper.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java b/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java index f74bd28..2414faf 100644 --- a/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java +++ b/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java @@ -16,17 +16,25 @@ public class ResponseResult { } public static ResponseObject success() { - return success(0L, (Object)null); + return success(0L, (Object) null); } public static ResponseObject success(T data) { return success(1L, data); } + public static ResponseObject success(T data, String msg) { + return success(1L, data, msg); + } + public static ResponseObject success(long total, T data) { return ResponseObject.builder().code(HttpCode.OK.value()).msg(HttpCode.OK.getReasonPhrase()).count(total).data(data).timestamp(System.currentTimeMillis()).build(); } + public static ResponseObject success(long total, T data, String msg) { + return ResponseObject.builder().code(HttpCode.OK.value()).msg(msg).count(total).data(data).timestamp(System.currentTimeMillis()).build(); + } + public static ResponseObject error(HttpCode responseCode, String errorMsg) { return error(responseCode, errorMsg, 0L, null); } diff --git a/stater/zyjblogs-rabbitmq-spring-boot-starter/pom.xml b/stater/zyjblogs-rabbitmq-spring-boot-starter/pom.xml new file mode 100644 index 0000000..64f58b0 --- /dev/null +++ b/stater/zyjblogs-rabbitmq-spring-boot-starter/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + + zyjblogs-parent + cn.zyjblogs + 1.0-SNAPSHOT + + + cn.zyjblogs.starter + zyjblogs-rabbitmq-spring-boot-starter + 1.0-SNAPSHOT + jar + + + 8 + 8 + + + + cn.zyjblogs.starter + zyjblogs-common-spring-boot-starter + ${zyjblogs.version} + + + + org.springframework.boot + spring-boot-autoconfigure + + + org.springframework.boot + spring-boot-configuration-processor + + + cn.zyjblogs.starter + zyjblogs-redis-spring-boot-starter + ${zyjblogs.version} + + + org.springframework.boot + spring-boot-starter-amqp + + + + \ No newline at end of file