作者 朱兆平

redis缓存

... ... @@ -8,5 +8,5 @@ EXPOSE 8066
ENTRYPOINT ["java","-jar","/app.jar"]
# Ubuntu 时区
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
\ No newline at end of file
# Ubuntu 时区 同步主机与docker容器时间
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
\ No newline at end of file
... ...
... ... @@ -11,9 +11,9 @@
</parent>
<groupId>com.tianbo</groupId>
<artifactId>warehouse</artifactId>
<version>2.0Beta</version>
<version>2.2Beta</version>
<name>warehouse</name>
<description>warehouse for Spring Boot</description>
<description>usercenter for springcloud</description>
<properties>
<java.version>1.8</java.version>
... ...
... ... @@ -18,6 +18,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
... ... @@ -32,6 +33,7 @@ import java.util.List;
import java.util.Map;
@RestController
@Slf4j
@RequestMapping("/user")
@Api("swaggerDemoController相关的api")
public class UserController {
... ... @@ -133,16 +135,25 @@ public class UserController {
String authHeader = request.getHeader("Authorization");
if (authHeader != null && authHeader.startsWith("Bearer ")) {
final String authToken = authHeader.substring("Bearer ".length());
String username = JwtTokenUtil.parseToken(authToken);
//有JWT 没有登录,去JWT的 信息 获取用户信息,赋予登录
if (username != null) {
UserDetails userDetails = userDetailService.loadUserByUsername(username);
if (userDetails != null) {
String json = JSON.toJSONString(userDetails);
redisUtils.set(authToken, json);
return new ResultJson("200","缓存更新成功");
try {
String userJson = redisUtils.get(authToken);
if (userJson != null) {
USERS u = JSON.parseObject(userJson, USERS.class);
String username = u.getUsername();
// String username = JwtTokenUtil.parseToken(authToken);
if (username != null) {
UserDetails userDetails = userDetailService.loadUserByUsername(username);
if (userDetails != null) {
String json = JSON.toJSONString(userDetails);
redisUtils.set(authToken, json, 3600 * 24 * 7);
return new ResultJson("200", "缓存更新成功");
}
}
}
}catch (Exception e){
log.error(e.toString());
return new ResultJson("500","缓存更新失败");
}
}
return new ResultJson("500","缓存更新失败");
... ...
... ... @@ -77,7 +77,7 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter{
}
}else{
log.warn("token验证未通过{}",authHeader);
}
filterChain.doFilter(request, response);
... ...