Skip to content

Commit 9554f40

Browse files
🇨🇳 1.3.0.RELEASE shiro+jwt
1 parent fc1114c commit 9554f40

27 files changed

+250
-259
lines changed

src/main/java/io/geekidea/springbootplus/common/aop/LogAop.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.alibaba.fastjson.JSONObject;
2121
import io.geekidea.springbootplus.common.api.ApiCode;
2222
import io.geekidea.springbootplus.common.api.ApiResult;
23+
import io.geekidea.springbootplus.shiro.util.JwtTokenUtil;
2324
import io.geekidea.springbootplus.util.AnsiUtil;
2425
import io.geekidea.springbootplus.util.DateUtil;
2526
import io.geekidea.springbootplus.util.IpUtil;
@@ -131,7 +132,7 @@ public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
131132
map.put("time", DateUtil.getYYYYMMDDHHMMSS(new Date()));
132133

133134
// 获取请求头token
134-
map.put("x-auth-token",request.getHeader("x-auth-token"));
135+
map.put("token",request.getHeader(JwtTokenUtil.getTokenName()));
135136

136137
String requestInfo = null;
137138
try {

src/main/java/io/geekidea/springbootplus/common/constant/CommonConstant.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ public interface CommonConstant {
4141
/**
4242
* 登陆token
4343
*/
44-
String JWT_TOKEN_NAME = "token";
44+
String JWT_DEFAULT_TOKEN_NAME = "token";
4545

4646
/**
4747
* JWT用户名
4848
*/
4949
String JWT_USERNAME = "username";
5050

51-
5251
/**
5352
* JWT刷新新token响应状态码
5453
*/
@@ -60,6 +59,15 @@ public interface CommonConstant {
6059
*/
6160
int JWT_INVALID_TOKEN_CODE = 461;
6261

62+
/**
63+
* JWT Token默认密钥
64+
*/
65+
String JWT_DEFAULT_SECRET = "666666";
66+
67+
/**
68+
* JWT 默认过期时间,3600L,单位秒
69+
*/
70+
Long JWT_DEFAULT_EXPIRE_SECOND = 3600L;
6371

6472
/**
6573
* 初始密码

src/main/java/io/geekidea/springbootplus/common/web/filter/CrossDomainFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
4646
httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");
4747
httpServletResponse.setHeader("Access-Control-Allow-Headers", "*");
4848
httpServletResponse.setHeader("Access-Control-Request-Headers","*");
49-
httpServletResponse.setHeader("Access-Control-Allow-Headers", "content-type,x-auth-token");
49+
httpServletResponse.setHeader("Access-Control-Allow-Headers", "content-type,token");
5050
httpServletResponse.setHeader("Access-Control-Expose-Headers", "*");
5151

5252
HttpServletRequest request = (HttpServletRequest) servletRequest;

src/main/java/io/geekidea/springbootplus/config/Swagger2Config.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.geekidea.springbootplus.config;
1818

19+
import io.geekidea.springbootplus.shiro.util.JwtTokenUtil;
1920
import org.springframework.beans.factory.annotation.Value;
2021
import org.springframework.context.annotation.Bean;
2122
import org.springframework.context.annotation.Configuration;
@@ -113,10 +114,17 @@ private ApiInfo apiInfo() {
113114
private List<Parameter> setHeaderToken() {
114115
List<Parameter> pars = new ArrayList<>();
115116

116-
// TODO 测试token值,上线关闭
117+
// token请求头
117118
String testTokenValue = "";
118119
ParameterBuilder tokenPar = new ParameterBuilder();
119-
Parameter tokenParameter = tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).defaultValue(testTokenValue).build();
120+
Parameter tokenParameter = tokenPar
121+
.name(JwtTokenUtil.getTokenName())
122+
.description("Token Request Header")
123+
.modelRef(new ModelRef("string"))
124+
.parameterType("header")
125+
.required(false)
126+
.defaultValue(testTokenValue)
127+
.build();
120128
pars.add(tokenParameter);
121129
return pars;
122130
}

src/main/java/io/geekidea/springbootplus/core/SpringBootPlusProperties.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ public class SpringBootPlusProperties {
5151
*/
5252
private boolean responseLogFormat;
5353

54-
/**
55-
* 登录token失效时间,单位分钟,默认60分钟失效
56-
*/
57-
private Integer tokenValidTime = 60;
58-
5954
/**
6055
* 拦截器配置
6156
*/

src/main/java/io/geekidea/springbootplus/shiro/cache/LoginRedisService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ public interface LoginRedisService {
5656
/**
5757
* 删除对应用户的Redis缓存
5858
*
59-
* @param jwtToken
59+
* @param token
60+
* @param username
6061
*/
61-
void deleteLoginInfo(JwtToken jwtToken);
62+
void deleteLoginInfo(String token, String username);
6263

6364
/**
6465
* 判断token在redis中是否存在

src/main/java/io/geekidea/springbootplus/shiro/cache/impl/LoginRedisServiceImpl.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.time.Duration;
3333

3434
/**
35+
* 登陆信息Redis缓存服务类
36+
*
3537
* @author geekidea
3638
* @date 2019-09-30
3739
* @since 1.3.0.RELEASE
@@ -88,10 +90,6 @@ public void cacheLoginInfo(JwtToken jwtToken, LoginSysUserVo loginSysUserVo, boo
8890
redisTemplate.opsForValue().set(String.format(CommonRedisKey.LOGIN_USER, username), loginSysUserRedisVo, expireDuration);
8991
// 3. salt hash,方便获取盐值鉴权
9092
redisTemplate.opsForValue().set(String.format(CommonRedisKey.LOGIN_SALT, username), salt, expireDuration);
91-
if (generate) {
92-
// 4. username hash,统计用户登陆次数
93-
redisTemplate.opsForHash().increment(CommonRedisKey.LOGIN_USER_HASH, username, 1);
94-
}
9593

9694
}
9795

@@ -122,21 +120,20 @@ public String getSalt(String username) {
122120
}
123121

124122
@Override
125-
public void deleteLoginInfo(JwtToken jwtToken) {
126-
if (jwtToken == null) {
127-
throw new IllegalArgumentException("jwtToken不能为空");
123+
public void deleteLoginInfo(String token, String username) {
124+
if (token == null) {
125+
throw new IllegalArgumentException("token不能为空");
126+
}
127+
if (username == null) {
128+
throw new IllegalArgumentException("username不能为空");
128129
}
129-
String username = jwtToken.getUsername();
130-
String token = jwtToken.getToken();
131130
String tokenMd5 = DigestUtils.md5Hex(token);
132131
// 1. delete tokenMd5
133132
redisTemplate.delete(String.format(CommonRedisKey.LOGIN_TOKEN, tokenMd5));
134133
// 2. delete username
135134
redisTemplate.delete(String.format(CommonRedisKey.LOGIN_USER, username));
136135
// 3. delete salt
137136
redisTemplate.delete(String.format(CommonRedisKey.LOGIN_SALT, username));
138-
// 4. delete username hash
139-
redisTemplate.opsForHash().increment(CommonRedisKey.LOGIN_USER_HASH, username, -1);
140137
}
141138

142139
@Override

src/main/java/io/geekidea/springbootplus/shiro/controller/LoginController.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@
1414
package io.geekidea.springbootplus.shiro.controller;
1515

1616
import io.geekidea.springbootplus.common.api.ApiResult;
17-
import io.geekidea.springbootplus.shiro.jwt.JwtProperties;
18-
import io.geekidea.springbootplus.shiro.jwt.JwtToken;
1917
import io.geekidea.springbootplus.shiro.cache.LoginRedisService;
18+
import io.geekidea.springbootplus.shiro.jwt.JwtProperties;
2019
import io.geekidea.springbootplus.shiro.param.LoginParam;
2120
import io.geekidea.springbootplus.shiro.service.LoginService;
2221
import io.geekidea.springbootplus.system.web.vo.SysUserQueryVo;
2322
import io.swagger.annotations.Api;
2423
import io.swagger.annotations.ApiOperation;
2524
import lombok.extern.slf4j.Slf4j;
26-
import org.apache.shiro.SecurityUtils;
27-
import org.apache.shiro.subject.Subject;
2825
import org.springframework.beans.factory.annotation.Autowired;
2926
import org.springframework.web.bind.annotation.PostMapping;
3027
import org.springframework.web.bind.annotation.RequestBody;
3128
import org.springframework.web.bind.annotation.RestController;
3229

30+
import javax.servlet.http.HttpServletRequest;
3331
import javax.servlet.http.HttpServletResponse;
3432
import javax.validation.Valid;
3533

@@ -61,14 +59,8 @@ public ApiResult login(@Valid @RequestBody LoginParam loginParam, HttpServletRes
6159
}
6260

6361
@PostMapping("/logout")
64-
public ApiResult logout() {
65-
Subject subject = SecurityUtils.getSubject();
66-
//注销
67-
subject.logout();
68-
// 删除Redis缓存信息
69-
JwtToken jwtToken = (JwtToken) subject.getPrincipal();
70-
System.out.println("jwtToken = " + jwtToken);
71-
loginRedisService.deleteLoginInfo(jwtToken);
62+
public ApiResult logout(HttpServletRequest request) {
63+
loginService.logout(request);
7264
return ApiResult.ok("退出成功");
7365
}
7466
}

src/main/java/io/geekidea/springbootplus/shiro/convert/ShiroMapstructConvert.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.mapstruct.factory.Mappers;
2222

2323
/**
24+
* Shiro包下使用mapstruct对象属性复制转换器
25+
*
2426
* @author geekidea
2527
* @date 2019-09-30
2628
* @since 1.3.0.RELEASE
@@ -30,7 +32,20 @@ public interface ShiroMapstructConvert {
3032

3133
ShiroMapstructConvert INSTANCE = Mappers.getMapper(ShiroMapstructConvert.class);
3234

35+
/**
36+
* LoginSysUserVo对象转换成LoginSysUserRedisVo
37+
*
38+
* @param loginSysUserVo
39+
* @return
40+
*/
3341
LoginSysUserRedisVo loginSysUserVoToLoginSysUserRedisVo(LoginSysUserVo loginSysUserVo);
3442

43+
/**
44+
* JwtToken对象转换成JwtTokenRedisVo
45+
*
46+
* @param jwtToken
47+
* @return
48+
*/
3549
JwtTokenRedisVo jwtTokenToJwtTokenRedisVo(JwtToken jwtToken);
50+
3651
}

src/main/java/io/geekidea/springbootplus/shiro/jwt/JwtCredentialsMatcher.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313

1414
package io.geekidea.springbootplus.shiro.jwt;
1515

16-
import io.geekidea.springbootplus.shiro.cache.LoginRedisService;
1716
import io.geekidea.springbootplus.shiro.util.JwtUtil;
1817
import lombok.extern.slf4j.Slf4j;
1918
import org.apache.shiro.authc.AuthenticationInfo;
2019
import org.apache.shiro.authc.AuthenticationToken;
2120
import org.apache.shiro.authc.credential.CredentialsMatcher;
22-
import org.springframework.beans.factory.annotation.Autowired;
2321

2422
/**
2523
* JWT证书匹配
@@ -31,18 +29,14 @@
3129
@Slf4j
3230
public class JwtCredentialsMatcher implements CredentialsMatcher {
3331

34-
@Autowired
35-
private LoginRedisService loginRedisService;
36-
3732
@Override
3833
public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
3934
String token = authenticationToken.getCredentials().toString();
4035
String salt = authenticationInfo.getCredentials().toString();
4136
try {
42-
boolean success = JwtUtil.verifyToken(token, salt);
43-
return success;
37+
return JwtUtil.verifyToken(token, salt);
4438
} catch (Exception e) {
45-
log.error("Token CredentialsMatch Exception:" + e.getMessage(), e);
39+
log.error("JWT Token CredentialsMatch Exception:" + e.getMessage(), e);
4640
}
4741
return false;
4842
}

0 commit comments

Comments
 (0)