@PostMapping("/register")
@RequiresPermissions(value = {"user.register"},logical = Logical.OR)
public ResponseBean add(@RequestBody @Valid AddUserReqVo vo) {
User add = userService.add(vo);
if (ObjectUtils.isNull(add)) {
return ResponseBean.setResponseMessage(Const.FAILED_CODE, Const.REQUEST_FAILED, Const.USER_ERROR_USERNAME);
}
Map<String, Object> data = new HashMap<>();
if (add != null) {
data.put("userId", add.getUserId());
return ResponseBean.setResponseMessage(Const.SUCCESS_CODE, Const.REQUEST_SUCCESS, data);
}
return ResponseBean.setResponseMessage(Const.FAILED_CODE, Const.USER_ERROR_USERNAME, vo.getUsername());
}
```java
{
"message": "java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class org.apache.shiro.authz.AuthorizationInfo (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; org.apache.shiro.authz.AuthorizationInfo is in unnamed module of loader 'app'): class java.util.LinkedHashMap cannot be cast to class org.apache.shiro.authz.AuthorizationInfo (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; org.apache.shiro.authz.AuthorizationInfo is in unnamed module of loader 'app')",
"code": 500,
"data": null
}
package com.fahuobang.shiro.config;
import com.fahuobang.common.cache.CustomCacheManager;
import com.fahuobang.shiro.jwt.JwtFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* shiro配置类
*/
@Slf4j
@Configuration
public class ShiroConfig {
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Bean("securityManager")
public DefaultWebSecurityManager defaultWebSecurityManager(UserRealm userRealm) {
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
// 使用自定义Realm
defaultWebSecurityManager.setRealm(userRealm);
// 关闭Shiro自带的session
DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
defaultWebSecurityManager.setSubjectDAO(subjectDAO);
// 设置自定义Cache缓存
defaultWebSecurityManager.setCacheManager(new CustomCacheManager());
return defaultWebSecurityManager;
}
@Bean("shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
// 添加自己的过滤器取名为jwt
Map<String, Filter> filterMap = new HashMap<>(16);
filterMap.put("jwt", new JwtFilter());
factoryBean.setFilters(filterMap);
factoryBean.setSecurityManager(securityManager);
// 自定义url规则使用LinkedHashMap有序Map
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(16);
// 登录接口放开
map.put("/user/login", "anon");
map.put("/logout","logout");
// 所有请求通过我们自己的JWTFilter
map.put("/**", "jwt");
factoryBean.setFilterChainDefinitionMap(map);
return factoryBean;
}
/**
* 下面的代码是添加注解支持
*/
@Bean
@DependsOn("lifecycleBeanPostProcessor")
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
// 强制使用cglib,防止重复代理和可能引起代理出错的问题,https://zhuanlan.zhihu.com/p/29161098
defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
return defaultAdvisorAutoProxyCreator;
}
// java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to
// class org.apache.shiro.authz.AuthorizationInfo (java.util.LinkedHashMap is in module java.base of
// loader 'bootstrap'; org.apache.shiro.authz.AuthorizationInfo is in unnamed module of loader 'app'):
// class java.util.LinkedHashMap cannot be cast to class org.apache.shiro.authz.AuthorizationInfo (java.util.LinkedHashMap is
// in module java.base of loader 'bootstrap'; org.apache.shiro.authz.AuthorizationInfo is in unnamed module of loader 'app
@Bean
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
advisor.setSecurityManager(securityManager);
return advisor;
}
}
```