新增ResponseResult中成功返回体方法

This commit is contained in:
zhuyijun 2022-10-10 20:11:26 +08:00
parent abe24f9e8a
commit bf24b36c62
8 changed files with 131 additions and 12 deletions

View File

@ -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);
}
}

View File

@ -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;

View File

@ -25,6 +25,7 @@ import java.util.Collection;
public class OauthAuthenticationProvider extends DaoAuthenticationProvider {
private UserDetailsService userDetailsService;
private PasswordEncoder passwordEncoder;
public OauthAuthenticationProvider(UserDetailsService userDetailsService,
PasswordEncoder passwordEncoder
) {
@ -52,7 +53,7 @@ public class OauthAuthenticationProvider extends DaoAuthenticationProvider {
//比较前端传入的密码明文和数据库中加密的密码是否相等
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<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
@ -61,8 +62,8 @@ public class OauthAuthenticationProvider extends DaoAuthenticationProvider {
}
/**
*
* 认证
*
* @param userDetails
* @param authentication
* @throws AuthenticationException

View File

@ -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<String> scope, Map<String, Object> addition) {
this.value = value;
public OAuth2AccessTokenVo(String value, String tokenType, String refreshToken, int expiresIn, Date expiration, Set<String> scope, Map<String, Object> 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;
}

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.zyjblogs.server.user.mapper.UserMapper">
<select id="findUserByname" resultType="cn.zyjblogs.server.user.po.UserPo">
select *
from user
where deleted = 0
and username like CONCAT('%', #{username}, '%')
<if test="tenantId != null and tenantId != '' ">
and tenant_id = #{tenantId}
</if>
</select>
</mapper>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.zyjblogs.server.user.mapper.UserMapper">
<select id="findUserByname" resultType="cn.zyjblogs.server.user.po.UserPo">
select *
from user
where deleted = 0
and username like CONCAT('%', #{username}, '%')
<if test="tenantId != null and tenantId != '' ">
and tenant_id = #{tenantId}
</if>
</select>
</mapper>

View File

@ -23,10 +23,18 @@ public class ResponseResult {
return success(1L, data);
}
public static <T> ResponseObject<T> success(T data, String msg) {
return success(1L, data, msg);
}
public static <T> ResponseObject<T> success(long total, T data) {
return ResponseObject.<T>builder().code(HttpCode.OK.value()).msg(HttpCode.OK.getReasonPhrase()).count(total).data(data).timestamp(System.currentTimeMillis()).build();
}
public static <T> ResponseObject<T> success(long total, T data, String msg) {
return ResponseObject.<T>builder().code(HttpCode.OK.value()).msg(msg).count(total).data(data).timestamp(System.currentTimeMillis()).build();
}
public static <T> ResponseObject<T> error(HttpCode responseCode, String errorMsg) {
return error(responseCode, errorMsg, 0L, null);
}

View File

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>zyjblogs-parent</artifactId>
<groupId>cn.zyjblogs</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath/>
</parent>
<groupId>cn.zyjblogs.starter</groupId>
<artifactId>zyjblogs-rabbitmq-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>cn.zyjblogs.starter</groupId>
<artifactId>zyjblogs-common-spring-boot-starter</artifactId>
<version>${zyjblogs.version}</version>
</dependency>
<!-- Spring 集成 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>cn.zyjblogs.starter</groupId>
<artifactId>zyjblogs-redis-spring-boot-starter</artifactId>
<version>${zyjblogs.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>