fix: 日志入库

This commit is contained in:
wangyu 2021-01-15 21:49:24 +08:00
parent 5d90c5919d
commit 39f57e2529
4 changed files with 17 additions and 16 deletions

View File

@ -1,7 +1,7 @@
package com.flyfish.framework.config; package com.flyfish.framework.config;
import com.flyfish.framework.config.properties.JwtProperties;
import com.flyfish.framework.config.properties.SecurityProperties; import com.flyfish.framework.config.properties.SecurityProperties;
import com.flyfish.framework.configuration.jwt.JwtProperties;
import com.flyfish.framework.configuration.jwt.JwtSecurityContextRepository; import com.flyfish.framework.configuration.jwt.JwtSecurityContextRepository;
import com.flyfish.framework.configuration.jwt.TokenProvider; import com.flyfish.framework.configuration.jwt.TokenProvider;
import com.flyfish.framework.domain.UserQo; import com.flyfish.framework.domain.UserQo;
@ -69,10 +69,7 @@ public class WebSecurityConfig {
@Bean @Bean
public TokenProvider tokenProvider(JwtProperties properties) { public TokenProvider tokenProvider(JwtProperties properties) {
return new TokenProvider( return new TokenProvider(properties);
properties.getBase64Secret(),
properties.getTokenValidityInSeconds(),
properties.getTokenValidityInSecondsForRememberMe());
} }
@Bean @Bean

View File

@ -1,4 +1,4 @@
package com.flyfish.framework.config.properties; package com.flyfish.framework.configuration.jwt;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
@ -10,12 +10,14 @@ import java.util.Map;
* *
* @author wangyu * @author wangyu
*/ */
@ConfigurationProperties(prefix = "jwt")
@Data @Data
@ConfigurationProperties(prefix = "jwt")
public class JwtProperties { public class JwtProperties {
// 是否启用 // 是否启用
private boolean enable = false; private boolean enable = false;
// 记住我
private boolean remember = false;
// 头部 // 头部
private String header = "Authorization"; private String header = "Authorization";
// This token must be encoded using Base64 with mininum 88 Bits (you can type `echo 'secret-key'|base64` on your command line) // This token must be encoded using Base64 with mininum 88 Bits (you can type `echo 'secret-key'|base64` on your command line)

View File

@ -2,6 +2,7 @@ package com.flyfish.framework.configuration.jwt;
import com.flyfish.framework.domain.base.IUser; import com.flyfish.framework.domain.base.IUser;
import com.flyfish.framework.utils.RedisOperations; import com.flyfish.framework.utils.RedisOperations;
import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm;
import io.jsonwebtoken.*; import io.jsonwebtoken.*;
import io.jsonwebtoken.io.Decoders; import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.io.DecodingException; import io.jsonwebtoken.io.DecodingException;
@ -11,7 +12,6 @@ import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
@ -38,24 +38,26 @@ public class TokenProvider implements InitializingBean {
public static final String AUTHORIZATION_HEADER = "Authorization"; public static final String AUTHORIZATION_HEADER = "Authorization";
private static final String AUTHORITIES_KEY = "auth"; private static final String AUTHORITIES_KEY = "auth";
private final String base64Secret; private final Boolean remember;
private final long tokenValidityInMilliseconds; private final long tokenValidityInMilliseconds;
private final long tokenValidityInMillisecondsForRememberMe; private final long tokenValidityInMillisecondsForRememberMe;
@Resource @Resource
private RedisOperations redisOperations; private RedisOperations redisOperations;
@Resource
private JwtProperties jwtProperties;
private Key key; private Key key;
public TokenProvider(String base64Secret, long tokenValidityInSeconds, long tokenValidityInSecondsForRememberMe) { public TokenProvider(JwtProperties jwtProperties) {
this.base64Secret = base64Secret; this.remember = jwtProperties.isRemember();
this.tokenValidityInMilliseconds = tokenValidityInSeconds * 1000; this.tokenValidityInMilliseconds = jwtProperties.getTokenValidityInSeconds() * 1000;
this.tokenValidityInMillisecondsForRememberMe = tokenValidityInSecondsForRememberMe * 1000; this.tokenValidityInMillisecondsForRememberMe = jwtProperties.getTokenValidityInSecondsForRememberMe() * 1000;
} }
@Override @Override
public void afterPropertiesSet() { public void afterPropertiesSet() {
byte[] keyBytes = Decoders.BASE64.decode(base64Secret); byte[] keyBytes = Decoders.BASE64.decode(jwtProperties.getBase64Secret());
this.key = Keys.hmacShaKeyFor(keyBytes); this.key = Keys.hmacShaKeyFor(keyBytes);
} }
@ -83,7 +85,7 @@ public class TokenProvider implements InitializingBean {
public void addToken(ServerWebExchange exchange, Authentication authentication) { public void addToken(ServerWebExchange exchange, Authentication authentication) {
IUser user = (IUser) authentication.getPrincipal(); IUser user = (IUser) authentication.getPrincipal();
String token = createToken(authentication, true); String token = createToken(authentication, remember);
HttpHeaders headers = exchange.getResponse().getHeaders(); HttpHeaders headers = exchange.getResponse().getHeaders();
// app用户从头部返回方便获取 // app用户从头部返回方便获取
headers.add("Token", token); headers.add("Token", token);