一 SpringBoot中开启Shiro 注解
ShiroConfig.java
@Bean
public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
advisorAutoProxyCreator.setProxyTargetClass(true);
return advisorAutoProxyCreator;
}
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}
SecurityManager 报红问题
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5a434727c3e4fc9c5dcfb8488515b27a.png)
解决SecurityManager 报红问题
import org.apache.shiro.mgt.SecurityManager;
二 SpringBootShiro 注解详解
注解 | 含义 |
---|
@RequiresAuthentication | 验证用户是否登录,等同于方法subject.isAuthenticated() 结果为true时。 |
@RequiresUser | 验证用户是否被记忆,user有两种含义:一种是成功登录的(subject.isAuthenticated() 结果为true);另外一种是被记忆的(subject.isRemembered()结果为true)。 |
@RequiresRoles | 例如:@RequiresRoles(“aRoleName”); void someMethod(); 如果subject中有aRoleName角色才可以访问方法someMethod。如果没有这个权限则会抛出异常AuthorizationException。 |
@RequiresPermissions | 例如: @RequiresPermissions({“file:read”, “write:aFile.txt”} ) void someMethod(); 要求subject中必须同时含有file:read和write:aFile.txt的权限才能执行方法someMethod()。否则抛出异常AuthorizationException。 |
@RequiresGuest | 验证是否是一个guest的请求,与@RequiresUser完全相反。 换言之,RequiresUser == !RequiresGuest。此时subject.getPrincipal() 结果为null. |