security config
This commit is contained in:
24
pom.xml
24
pom.xml
@@ -25,6 +25,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.mysql</groupId>
|
<groupId>com.mysql</groupId>
|
||||||
@@ -41,6 +45,26 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mapstruct</groupId>
|
||||||
|
<artifactId>mapstruct</artifactId>
|
||||||
|
<version>1.5.3.Final</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-api</artifactId>
|
||||||
|
<version>0.11.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-impl</artifactId>
|
||||||
|
<version>0.11.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-jackson</artifactId>
|
||||||
|
<version>0.11.5</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.wuyiqi.netstateproc.filter;
|
||||||
|
|
||||||
|
import com.wuyiqi.netstateproc.utils.JwtUtil;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
String token = request.getHeader("Authorization");
|
||||||
|
|
||||||
|
if(!StringUtils.hasText(token) || !token.startsWith("Bearer")) {
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
token = token.substring(7);
|
||||||
|
|
||||||
|
String userid;
|
||||||
|
try{
|
||||||
|
Claims claims = JwtUtil.parseJWT(token);
|
||||||
|
userid = claims.getSubject();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.wuyiqi.netstateproc.security.config;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebSecurity
|
||||||
|
public class SecurityConfig {
|
||||||
|
// @Autowired
|
||||||
|
// private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
|
||||||
|
}
|
||||||
63
src/main/java/com/wuyiqi/netstateproc/utils/JwtUtil.java
Normal file
63
src/main/java/com/wuyiqi/netstateproc/utils/JwtUtil.java
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package com.wuyiqi.netstateproc.utils;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import io.jsonwebtoken.JwtBuilder;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class JwtUtil {
|
||||||
|
public static final long JWT_TTL = 60 * 60 * 1000L * 24 * 2;
|
||||||
|
|
||||||
|
public static final String JWT_KEY = "ChengXuanWangZhaoLongWUYIQI666";
|
||||||
|
|
||||||
|
public static String getUUID() {
|
||||||
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String createJWT(String subject) {
|
||||||
|
JwtBuilder builder = getJwtBuilder(subject, null, getUUID());
|
||||||
|
return builder.compact();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) {
|
||||||
|
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
|
||||||
|
SecretKey secretKey = generalKey();
|
||||||
|
long nowMillis = System.currentTimeMillis();
|
||||||
|
Date now = new Date(nowMillis);
|
||||||
|
if (ttlMillis == null) {
|
||||||
|
ttlMillis = JwtUtil.JWT_TTL;
|
||||||
|
}
|
||||||
|
|
||||||
|
long expMillis = nowMillis + ttlMillis;
|
||||||
|
Date expDate = new Date(expMillis);
|
||||||
|
return Jwts.builder()
|
||||||
|
.setId(uuid)
|
||||||
|
.setSubject(subject)
|
||||||
|
.setIssuer("sg")
|
||||||
|
.setIssuedAt(now)
|
||||||
|
.signWith(secretKey, signatureAlgorithm)
|
||||||
|
.setExpiration(expDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SecretKey generalKey() {
|
||||||
|
byte[] encodeKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
|
||||||
|
return new SecretKeySpec(encodeKey, 0, encodeKey.length, "HmacSHA256");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Claims parseJWT(String jwt) throws Exception {
|
||||||
|
SecretKey secretKey = generalKey();
|
||||||
|
return Jwts.parserBuilder()
|
||||||
|
.setSigningKey(secretKey)
|
||||||
|
.build()
|
||||||
|
.parseClaimsJwt(jwt)
|
||||||
|
.getBody();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user