这篇文章主要介绍了如何在Spring boot加入shiro支持,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
在项目添加依赖
<!-- shiro spring. --><dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version></dependency><!-- shiro core --><dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version></dependency>配置文件目录下新建spring文件夹,在文件夹内新建spring-shiro.xml文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://monUtil.isEquals(user.getPwd(), pwd)) { //抛出凭证不正确异常 throw new IncorrectCredentialsException();}//上面都通过了就说明该用户存在并且密码相等// 验证成功了SecurityUtils.getSubject().getSession().setAttribute(Constant.SESSION_USER_KEY, user);// 返回shiro用户信息// token传过来的密码,一定要跟验证信息传进去的密码一致,加密的密码一定要加密后传过来 return new SimpleAuthenticationInfo(user, user.getPwd(), getName());如果要设置权限,就在对应的Controllerf方法加上
@RequiresPermissions("/system/user/list")再doGetAuthorizationInfo方法内写
//创建简单的授权信息对象SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();//授予权限simpleAuthorizationInfo.addStringPermission("/system/user/list"); return simpleAuthorizationInfo;当所有Controller都加了@RequiresPermissions注解后,如果访问到没有授权的Controller会报错。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。