Commit 77a4c715 authored by liqin's avatar liqin 💬

bug fixed

parent 64c7a548
...@@ -172,7 +172,7 @@ ...@@ -172,7 +172,7 @@
<dependency> <dependency>
<groupId>org.crazycake</groupId> <groupId>org.crazycake</groupId>
<artifactId>shiro-redis</artifactId> <artifactId>shiro-redis</artifactId>
<version>3.3.1</version> <version>3.3.2</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>org.apache.shiro</groupId> <groupId>org.apache.shiro</groupId>
......
...@@ -33,26 +33,7 @@ public class JwtFilter extends BasicHttpAuthenticationFilter { ...@@ -33,26 +33,7 @@ public class JwtFilter extends BasicHttpAuthenticationFilter {
} }
/** /**
* * 执行登录认证
*/
@Override
protected boolean executeLogin(ServletRequest servletRequest, ServletResponse servletResponse) {
HttpServletRequest request = (HttpServletRequest) servletRequest;
String authorization = request.getHeader("Authorization");
if (StringUtils.isNotBlank(authorization)) {
if (authorization.startsWith("Bearer ")) {
authorization = authorization.substring(7);
}
}
JwtToken token = new JwtToken(authorization);
// 提交给realm进行登入,如果错误他会抛出异常并被捕获
getSubject(servletRequest, servletResponse).login(token);
// 如果没有抛出异常则代表登入成功,返回true
return true;
}
/**
* 这里我们详细说明下为什么最终返回的都是true,即允许访问 * 这里我们详细说明下为什么最终返回的都是true,即允许访问
* 例如我们提供一个地址 GET /article * 例如我们提供一个地址 GET /article
* 登入用户和游客看到的内容是不同的 * 登入用户和游客看到的内容是不同的
...@@ -68,12 +49,32 @@ public class JwtFilter extends BasicHttpAuthenticationFilter { ...@@ -68,12 +49,32 @@ public class JwtFilter extends BasicHttpAuthenticationFilter {
try { try {
executeLogin(request, response); executeLogin(request, response);
} catch (UnauthorizedException | AuthenticationException e) { } catch (UnauthorizedException | AuthenticationException e) {
return false; throw new AuthenticationException("Token失效,请重新登录", e);
} }
} }
return true; return true;
} }
/**
*
*/
@Override
protected boolean executeLogin(ServletRequest servletRequest, ServletResponse servletResponse) {
HttpServletRequest request = (HttpServletRequest) servletRequest;
String authorization = request.getHeader("Authorization");
if (StringUtils.isNotBlank(authorization)) {
if (authorization.startsWith("Bearer ")) {
authorization = authorization.substring(7);
}
}
JwtToken token = new JwtToken(authorization);
// 提交给realm进行登入,如果错误他会抛出异常并被捕获
getSubject(servletRequest, servletResponse).login(token);
// 如果没有抛出异常则代表登入成功,返回true
return true;
}
@Override @Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) { protected boolean onAccessDenied(ServletRequest request, ServletResponse response) {
HttpServletResponse httpServletResponse = (HttpServletResponse) response; HttpServletResponse httpServletResponse = (HttpServletResponse) response;
......
...@@ -68,21 +68,21 @@ public class MyShiroRealm extends AuthorizingRealm { ...@@ -68,21 +68,21 @@ public class MyShiroRealm extends AuthorizingRealm {
/** /**
* 认证信息.(身份验证) : Authentication 是用来验证用户身份 * 认证信息.(身份验证) : Authentication 是用来验证用户身份
*
* @param auth
* @return
* @throws AuthenticationException * @throws AuthenticationException
*/ */
@Override @Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) throws AuthenticationException { protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String token = (String) auth.getCredentials(); String credentials = (String) token.getCredentials();
if (credentials == null) {
throw new AuthenticationException("token为空!");
}
Boolean hasToken = stringRedisTemplate.hasKey(SHIRO_JWT_TOKEN + token); Boolean hasToken = stringRedisTemplate.hasKey(SHIRO_JWT_TOKEN + token);
if (hasToken == null || !hasToken) { if (hasToken == null || !hasToken) {
throw new AuthenticationException("用户未登录!"); throw new AuthenticationException("用户未登录!");
} }
LOGGER.info("MyShiroRealm doGetAuthenticationInfo().token=" + token); LOGGER.info("MyShiroRealm doGetAuthenticationInfo().token=" + token);
String username = JwtTokenUtil.getUsername(token); String username = JwtTokenUtil.getUsername(credentials);
if (username == null) { if (username == null) {
throw new AuthenticationException("token invalid"); throw new AuthenticationException("token invalid");
} }
...@@ -90,17 +90,17 @@ public class MyShiroRealm extends AuthorizingRealm { ...@@ -90,17 +90,17 @@ public class MyShiroRealm extends AuthorizingRealm {
// 通过username从数据库中查找 // 通过username从数据库中查找
// 实际项目中,这里可以根据实际情况做缓存,如果不做,Shiro自己也是有时间间隔机制,2分钟内不会重复执行该方法 // 实际项目中,这里可以根据实际情况做缓存,如果不做,Shiro自己也是有时间间隔机制,2分钟内不会重复执行该方法
String employeeId = JwtTokenUtil.getEmployeeId(token); String employeeId = JwtTokenUtil.getEmployeeId(credentials);
Employee employee = this.employeeService.selectByEmpId(employeeId); Employee employee = this.employeeService.selectByEmpId(employeeId);
if (employee == null) { if (employee == null) {
throw new AuthenticationException("User does not exist!"); throw new AuthenticationException("User does not exist!");
} }
if (JwtTokenUtil.verify(token, username) == null) { if (JwtTokenUtil.verify(credentials, username) == null) {
throw new AuthenticationException("token invalid"); throw new AuthenticationException("token invalid");
} }
return new SimpleAuthenticationInfo(new Employee(employee.getId(), token), token, getName()); return new SimpleAuthenticationInfo(new Employee(employee.getId(), credentials), token, getName());
} }
/** /**
...@@ -114,7 +114,6 @@ public class MyShiroRealm extends AuthorizingRealm { ...@@ -114,7 +114,6 @@ public class MyShiroRealm extends AuthorizingRealm {
if (hasToken == null || !hasToken) { if (hasToken == null || !hasToken) {
throw new AuthenticationException("token invalid!"); throw new AuthenticationException("token invalid!");
} }
String employeeId = JwtTokenUtil.getEmployeeId(employee.getJwtToken()); String employeeId = JwtTokenUtil.getEmployeeId(employee.getJwtToken());
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
......
...@@ -98,7 +98,7 @@ public class ShiroConfig { ...@@ -98,7 +98,7 @@ public class ShiroConfig {
return shiroFilterFactoryBean; return shiroFilterFactoryBean;
} }
@Bean @Bean("securityManager")
public DefaultWebSecurityManager securityManager() { public DefaultWebSecurityManager securityManager() {
logger.info("ShiroConfiguration.securityManager()"); logger.info("ShiroConfiguration.securityManager()");
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
...@@ -157,7 +157,7 @@ public class ShiroConfig { ...@@ -157,7 +157,7 @@ public class ShiroConfig {
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() { public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
logger.info("ShiroConfiguration.defaultAdvisorAutoProxyCreator()"); logger.info("ShiroConfiguration.defaultAdvisorAutoProxyCreator()");
DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
defaultAdvisorAutoProxyCreator.setUsePrefix(true); //defaultAdvisorAutoProxyCreator.setUsePrefix(true);
// 强制使用cglib,防止重复代理和可能引起代理出错的问题 // 强制使用cglib,防止重复代理和可能引起代理出错的问题
defaultAdvisorAutoProxyCreator.setProxyTargetClass(true); defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
return defaultAdvisorAutoProxyCreator; return defaultAdvisorAutoProxyCreator;
...@@ -201,10 +201,12 @@ public class ShiroConfig { ...@@ -201,10 +201,12 @@ public class ShiroConfig {
public RedisCacheManager redisCacheManager() { public RedisCacheManager redisCacheManager() {
RedisCacheManager redisCacheManager = new RedisCacheManager(); RedisCacheManager redisCacheManager = new RedisCacheManager();
redisCacheManager.setRedisManager(redisManager()); redisCacheManager.setRedisManager(redisManager());
redisCacheManager.setKeyPrefix(CACHE_KEY); //redisCacheManager.setKeyPrefix(CACHE_KEY);
// shiro-redis要求放在session里面的实体类必须有个id标识 // shiro-redis要求放在session里面的实体类必须有个id标识
//这是组成redis中所存储数据的key的一部分 //这是组成redis中所存储数据的key的一部分
redisCacheManager.setPrincipalIdFieldName("id"); redisCacheManager.setPrincipalIdFieldName("id");
//用户权限信息缓存时间
//redisCacheManager.setExpire(200000);
return redisCacheManager; return redisCacheManager;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment