Skip to content

Commit

Permalink
优化JwtFilter注释
Browse files Browse the repository at this point in the history
  • Loading branch information
dolyw committed Mar 18, 2019
1 parent 4969f95 commit 2ceb663
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
9 changes: 5 additions & 4 deletions src/main/java/com/wang/config/shiro/ShiroConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -66,11 +67,11 @@ public ShiroFilterFactoryBean factory(DefaultWebSecurityManager securityManager)
filterMap.put("jwt", new JwtFilter());
factoryBean.setFilters(filterMap);
factoryBean.setSecurityManager(securityManager);
// 自定义url规则
Map<String, String> filterRuleMap = new HashMap<>(16);
// 自定义url规则使用LinkedHashMap有序Map
LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>(16);
// 所有请求通过我们自己的JWTFilter
filterRuleMap.put("/**", "jwt");
factoryBean.setFilterChainDefinitionMap(filterRuleMap);
filterChainDefinitionMap.put("/**", "jwt");
factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return factoryBean;
}

Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/wang/config/shiro/UserRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal
// 查询用户角色
List<RoleDto> roleDtos = roleMapper.findRoleByUser(userDto);
for (RoleDto roleDto : roleDtos) {
// 添加角色
simpleAuthorizationInfo.addRole(roleDto.getName());
// 根据用户角色查询权限
List<PermissionDto> permissionDtos = permissionMapper.findPermissionByRole(roleDto);
for (PermissionDto permissionDto : permissionDtos) {
// 添加权限
simpleAuthorizationInfo.addStringPermission(permissionDto.getPerCode());
if (roleDto != null) {
// 添加角色
simpleAuthorizationInfo.addRole(roleDto.getName());
// 根据用户角色查询权限
List<PermissionDto> permissionDtos = permissionMapper.findPermissionByRole(roleDto);
for (PermissionDto permissionDto : permissionDtos) {
if (permissionDto != null) {
// 添加权限
simpleAuthorizationInfo.addStringPermission(permissionDto.getPerCode());
}
}
}
}
return simpleAuthorizationInfo;
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/wang/config/shiro/jwt/JwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.wang.util.common.JsonConvertUtil;
import com.wang.util.common.PropertiesUtil;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -44,7 +45,7 @@ public class JwtFilter extends BasicHttpAuthenticationFilter {
*/
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
// 判断用户是否想要登入
// 查看当前Header中是否携带Authorization属性(Token),有的话就进行登录认证授权
if (this.isLoginAttempt(request, response)) {
try {
// 进行Shiro的登录UserRealm
Expand Down Expand Up @@ -81,6 +82,21 @@ protected boolean isAccessAllowed(ServletRequest request, ServletResponse respon
this.response401(request, response, msg);
return false;
}
} else {
// 没有携带Token
HttpServletRequest httpRequest = WebUtils.toHttp(request);
// 获取当前请求类型
String httpMethod = httpRequest.getMethod();
// 获取当前请求URI
String requestURI = httpRequest.getRequestURI();
LOGGER.info("当前请求 {} Authorization属性(Token)为空 请求类型 {}", requestURI, httpMethod);

// mustLoginFlag = true 开启任何请求必须登录才可访问
Boolean mustLoginFlag = false;
if (mustLoginFlag) {
this.response401(request, response, "请先登录");
return false;
}
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/wang/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public ResponseBean info() {
// 获取当前登录用户Id
Integer id = userUtil.getUserId();
// 获取当前登录用户Token
String Token = userUtil.getToken();
String token = userUtil.getToken();
// 获取当前登录用户Account
String account = userUtil.getAccount();
return new ResponseBean(HttpStatus.OK.value(), "您已经登录了(You are already logged in)", userDto);
Expand Down

0 comments on commit 2ceb663

Please sign in to comment.