🚀 添加JWT
This commit is contained in:
parent
f88b4afd82
commit
7762d8f9fa
|
@ -0,0 +1,51 @@
|
||||||
|
package cn.bunny.common.utils;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.*;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class JwtHelper {
|
||||||
|
private static final long tokenExpiration = 365L * 24 * 60 * 60 * 1000;
|
||||||
|
private static final String tokenSignKey = "Bunny-Java-Template";
|
||||||
|
|
||||||
|
public static String createToken(Long userId, String userName) {
|
||||||
|
return Jwts.builder()
|
||||||
|
.setSubject("OA-USER")
|
||||||
|
.setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
|
||||||
|
.claim("userId", userId)
|
||||||
|
.claim("userName", userName)
|
||||||
|
.signWith(SignatureAlgorithm.HS256, tokenSignKey)
|
||||||
|
.compressWith(CompressionCodecs.GZIP)
|
||||||
|
.compact();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Long getUserId(String token) {
|
||||||
|
if (!StringUtils.hasText(token)) return null;
|
||||||
|
|
||||||
|
Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
|
||||||
|
Claims claims = claimsJws.getBody();
|
||||||
|
Integer userId = (Integer) claims.get("userId");
|
||||||
|
return userId.longValue();
|
||||||
|
// return 1L;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getUserName(String token) {
|
||||||
|
if (!StringUtils.hasText(token)) return "";
|
||||||
|
|
||||||
|
Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
|
||||||
|
Claims claims = claimsJws.getBody();
|
||||||
|
return (String) claims.get("userName");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeToken(String token) {
|
||||||
|
// jwttoken无需删除,客户端扔掉即可。
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String token = JwtHelper.createToken(7L, "admin");
|
||||||
|
System.out.println(token);
|
||||||
|
System.out.println(JwtHelper.getUserId(token));
|
||||||
|
System.out.println(JwtHelper.getUserName(token));
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,5 +23,9 @@
|
||||||
<artifactId>model</artifactId>
|
<artifactId>model</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -12,6 +12,7 @@ import org.springframework.security.config.annotation.method.configuration.Enabl
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||||
|
import org.springframework.security.core.session.SessionRegistry;
|
||||||
import org.springframework.security.core.session.SessionRegistryImpl;
|
import org.springframework.security.core.session.SessionRegistryImpl;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
|
||||||
|
@ -56,7 +57,7 @@ public class WebSecurityConfig {
|
||||||
// 最大登录数为1
|
// 最大登录数为1
|
||||||
session.maximumSessions(1)
|
session.maximumSessions(1)
|
||||||
// 可以获取到所有登录的用户,以及登录状态,设置session状态
|
// 可以获取到所有登录的用户,以及登录状态,设置session状态
|
||||||
.sessionRegistry(new SessionRegistryImpl())
|
.sessionRegistry(sessionRegistry())
|
||||||
// 有相同用户已登录时
|
// 有相同用户已登录时
|
||||||
.expiredSessionStrategy(new SecuritySessionInformationExpiredStrategy());
|
.expiredSessionStrategy(new SecuritySessionInformationExpiredStrategy());
|
||||||
// 会话失效,同时内容
|
// 会话失效,同时内容
|
||||||
|
@ -77,4 +78,9 @@ public class WebSecurityConfig {
|
||||||
|
|
||||||
return httpSecurity.build();
|
return httpSecurity.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public SessionRegistry sessionRegistry() {
|
||||||
|
return new SessionRegistryImpl();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
8
pom.xml
8
pom.xml
|
@ -31,6 +31,7 @@
|
||||||
<fastjson2.version>2.0.47</fastjson2.version>
|
<fastjson2.version>2.0.47</fastjson2.version>
|
||||||
<minio.version>8.5.9</minio.version>
|
<minio.version>8.5.9</minio.version>
|
||||||
<lombok.version>1.18.32</lombok.version>
|
<lombok.version>1.18.32</lombok.version>
|
||||||
|
<jwt.version>0.9.1</jwt.version>
|
||||||
<easyexcel.version>3.3.3</easyexcel.version>
|
<easyexcel.version>3.3.3</easyexcel.version>
|
||||||
<jodatime.version>2.10.1</jodatime.version>
|
<jodatime.version>2.10.1</jodatime.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
@ -78,6 +79,13 @@
|
||||||
<artifactId>hutool-all</artifactId>
|
<artifactId>hutool-all</artifactId>
|
||||||
<version>5.8.25</version>
|
<version>5.8.25</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!--jjwt-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt</artifactId>
|
||||||
|
<version>${jwt.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Excel表操作 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
<artifactId>easyexcel</artifactId>
|
<artifactId>easyexcel</artifactId>
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
package cn.bunny.service.service.impl;
|
||||||
|
|
||||||
|
import cn.bunny.entity.system.SysRole;
|
||||||
|
import cn.bunny.service.mapper.SysRoleMapper;
|
||||||
|
import cn.bunny.service.service.SysRoleService;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
|
||||||
|
}
|
Loading…
Reference in New Issue