新增ResponseResult中成功返回体方法
This commit is contained in:
parent
abe24f9e8a
commit
bf24b36c62
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package cn.zyjblogs.config.security;
|
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.constant.CommonRedisKeyConstant;
|
||||||
import cn.zyjblogs.starter.common.entity.response.HttpCode;
|
import cn.zyjblogs.starter.common.entity.response.HttpCode;
|
||||||
import cn.zyjblogs.starter.common.exception.AuthRuntimeException;
|
import cn.zyjblogs.starter.common.exception.AuthRuntimeException;
|
||||||
|
@ -25,9 +25,10 @@ import java.util.Collection;
|
|||||||
public class OauthAuthenticationProvider extends DaoAuthenticationProvider {
|
public class OauthAuthenticationProvider extends DaoAuthenticationProvider {
|
||||||
private UserDetailsService userDetailsService;
|
private UserDetailsService userDetailsService;
|
||||||
private PasswordEncoder passwordEncoder;
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
public OauthAuthenticationProvider(UserDetailsService userDetailsService,
|
public OauthAuthenticationProvider(UserDetailsService userDetailsService,
|
||||||
PasswordEncoder passwordEncoder
|
PasswordEncoder passwordEncoder
|
||||||
){
|
) {
|
||||||
this.passwordEncoder = passwordEncoder;
|
this.passwordEncoder = passwordEncoder;
|
||||||
this.userDetailsService = userDetailsService;
|
this.userDetailsService = userDetailsService;
|
||||||
this.setPasswordEncoder(passwordEncoder);
|
this.setPasswordEncoder(passwordEncoder);
|
||||||
@ -37,22 +38,22 @@ public class OauthAuthenticationProvider extends DaoAuthenticationProvider {
|
|||||||
@Override
|
@Override
|
||||||
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
||||||
String username = authentication.getName();
|
String username = authentication.getName();
|
||||||
if (authentication.getCredentials() == null){
|
if (authentication.getCredentials() == null) {
|
||||||
this.logger.debug("密码为空");
|
this.logger.debug("密码为空");
|
||||||
throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
|
throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
|
||||||
}
|
}
|
||||||
String password = (String) authentication.getCredentials();
|
String password = (String) authentication.getCredentials();
|
||||||
//获取用户信息
|
//获取用户信息
|
||||||
UserDetails user = userDetailsService.loadUserByUsername(username);
|
UserDetails user = userDetailsService.loadUserByUsername(username);
|
||||||
if (user == null){
|
if (user == null) {
|
||||||
this.logger.debug("用户不存在");
|
this.logger.debug("用户不存在");
|
||||||
throw new AuthRuntimeException(HttpCode.UNAUTHORIZED,"用户不存在");
|
throw new AuthRuntimeException(HttpCode.UNAUTHORIZED, "用户不存在");
|
||||||
}
|
}
|
||||||
OauthUserDetails userDetails = (OauthUserDetails) user;
|
OauthUserDetails userDetails = (OauthUserDetails) user;
|
||||||
//比较前端传入的密码明文和数据库中加密的密码是否相等
|
//比较前端传入的密码明文和数据库中加密的密码是否相等
|
||||||
if (!passwordEncoder.matches(password, userDetails.getPassword())) {
|
if (!passwordEncoder.matches(password, userDetails.getPassword())) {
|
||||||
this.logger.debug("密码不正确");
|
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();
|
Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
|
||||||
@ -61,8 +62,8 @@ public class OauthAuthenticationProvider extends DaoAuthenticationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* 认证
|
* 认证
|
||||||
|
*
|
||||||
* @param userDetails
|
* @param userDetails
|
||||||
* @param authentication
|
* @param authentication
|
||||||
* @throws AuthenticationException
|
* @throws AuthenticationException
|
||||||
|
@ -9,6 +9,7 @@ import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ -19,7 +20,7 @@ import java.util.Set;
|
|||||||
public class OAuth2AccessTokenVo implements Serializable {
|
public class OAuth2AccessTokenVo implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
@ApiModelProperty(value = "access token", dataType = "String", example = "abc.efg.hjk")
|
@ApiModelProperty(value = "access token", dataType = "String", example = "abc.efg.hjk")
|
||||||
private String value;
|
private String token;
|
||||||
|
|
||||||
@ApiModelProperty(value = "token类型", dataType = "String", example = "bearer")
|
@ApiModelProperty(value = "token类型", dataType = "String", example = "bearer")
|
||||||
private String token_type;
|
private String token_type;
|
||||||
@ -41,10 +42,12 @@ public class OAuth2AccessTokenVo implements Serializable {
|
|||||||
|
|
||||||
@ApiModelProperty(value = "用户账号", dataType = "String", example = "hangman")
|
@ApiModelProperty(value = "用户账号", dataType = "String", example = "hangman")
|
||||||
private String username;
|
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) {
|
public OAuth2AccessTokenVo(String value, String tokenType, String refreshToken, int expiresIn, Date expiration, Set<String> scope, Map<String, Object> addition) {
|
||||||
this.value = value;
|
this.token = value;
|
||||||
this.token_type = tokenType;
|
this.token_type = tokenType;
|
||||||
this.refresh_token = refreshToken;
|
this.refresh_token = refreshToken;
|
||||||
this.expires_in = expiresIn;
|
this.expires_in = expiresIn;
|
||||||
@ -52,6 +55,7 @@ public class OAuth2AccessTokenVo implements Serializable {
|
|||||||
this.userId = (String) addition.get(ContextKeyConstant.USER_ID_KEY);
|
this.userId = (String) addition.get(ContextKeyConstant.USER_ID_KEY);
|
||||||
this.username = (String) addition.get(ContextKeyConstant.USERNAME_KEY);
|
this.username = (String) addition.get(ContextKeyConstant.USERNAME_KEY);
|
||||||
this.jti = (String) addition.get("jti");
|
this.jti = (String) addition.get("jti");
|
||||||
|
this.expiration = expiration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static OAuth2AccessTokenVo TransferToken(OAuth2AccessToken token) {
|
public static OAuth2AccessTokenVo TransferToken(OAuth2AccessToken token) {
|
||||||
@ -60,12 +64,13 @@ public class OAuth2AccessTokenVo implements Serializable {
|
|||||||
token.getTokenType(),
|
token.getTokenType(),
|
||||||
token.getRefreshToken().getValue(),
|
token.getRefreshToken().getValue(),
|
||||||
token.getExpiresIn(),
|
token.getExpiresIn(),
|
||||||
|
token.getExpiration(),
|
||||||
token.getScope(),
|
token.getScope(),
|
||||||
token.getAdditionalInformation());
|
token.getAdditionalInformation());
|
||||||
BaseContext.set(ContextDto.builder()
|
BaseContext.set(ContextDto.builder()
|
||||||
.userId(oAuth2AccessTokenVo.getUserId())
|
.userId(oAuth2AccessTokenVo.getUserId())
|
||||||
.username(oAuth2AccessTokenVo.getUsername())
|
.username(oAuth2AccessTokenVo.getUsername())
|
||||||
.token(oAuth2AccessTokenVo.getValue())
|
.token(oAuth2AccessTokenVo.getToken())
|
||||||
.build());
|
.build());
|
||||||
return oAuth2AccessTokenVo;
|
return oAuth2AccessTokenVo;
|
||||||
}
|
}
|
||||||
|
@ -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>
|
@ -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>
|
@ -16,17 +16,25 @@ public class ResponseResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ResponseObject<Object> success() {
|
public static ResponseObject<Object> success() {
|
||||||
return success(0L, (Object)null);
|
return success(0L, (Object) null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> ResponseObject<T> success(T data) {
|
public static <T> ResponseObject<T> success(T data) {
|
||||||
return success(1L, data);
|
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) {
|
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();
|
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) {
|
public static <T> ResponseObject<T> error(HttpCode responseCode, String errorMsg) {
|
||||||
return error(responseCode, errorMsg, 0L, null);
|
return error(responseCode, errorMsg, 0L, null);
|
||||||
}
|
}
|
||||||
|
48
stater/zyjblogs-rabbitmq-spring-boot-starter/pom.xml
Normal file
48
stater/zyjblogs-rabbitmq-spring-boot-starter/pom.xml
Normal 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>
|
Loading…
Reference in New Issue
Block a user