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;
import com.flyfish.framework.config.properties.JwtProperties;
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.TokenProvider;
import com.flyfish.framework.domain.UserQo;
@ -69,10 +69,7 @@ public class WebSecurityConfig {
@Bean
public TokenProvider tokenProvider(JwtProperties properties) {
return new TokenProvider(
properties.getBase64Secret(),
properties.getTokenValidityInSeconds(),
properties.getTokenValidityInSecondsForRememberMe());
return new TokenProvider(properties);
}
@Bean

View File

@ -78,4 +78,4 @@
<version>${jjwt.version}</version>
</dependency>
</dependencies>
</project>
</project>

View File

@ -1,4 +1,4 @@
package com.flyfish.framework.config.properties;
package com.flyfish.framework.configuration.jwt;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ -10,12 +10,14 @@ import java.util.Map;
*
* @author wangyu
*/
@ConfigurationProperties(prefix = "jwt")
@Data
@ConfigurationProperties(prefix = "jwt")
public class JwtProperties {
// 是否启用
private boolean enable = false;
// 记住我
private boolean remember = false;
// 头部
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)

View File

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