【6】shiro示例代码
坐标<!--shiro和spring整合--><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.3.2</version></dependency><!--shiro核心包--><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.3.2</version></dependency>配置文件:@Configurationpublic class ShiroConfig {//1.创建realm@Beanpublic PurviewRealm getRealm() {return new PurviewRealm();}//2.创建安全管理器@Beanpublic SecurityManager getSecurityManager(PurviewRealm realm) {DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();securityManager.setRealm(realm);return securityManager;}//3.配置shiro的过滤器工厂再web程序中,shiro进行权限控制全部是通过一组过滤器集合进行控制@Beanpublic ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {//1.创建过滤器工厂ShiroFilterFactoryBean filterFactory = new ShiroFilterFactoryBean();//2.设置安全管理器filterFactory.setSecurityManager(securityManager);//3.通用配置(跳转登录页面,为授权跳转的页面)filterFactory.setLoginUrl("/autherror");//跳转url地址//4.设置过滤器集合//key = 拦截的url地址//value = https://www.isolves.com/it/cxkf/kj/2022-09-19/过滤器类型Maprealm/** * 自定义的realm */public class PurviewRealm extends AuthorizingRealm {public void setName(String name) {super.setName("purviewRealm");}@Autowiredprivate UserService userService;/*** 授权方法* 操作的时候,判断用户是否具有响应的权限* 一定先认证再授权* 先认证 -- 安全数据* 再授权 -- 根据安全数据获取用户具有的所有操作权限*/protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {//1.获取已认证的用户数据User user = (User) principalCollection.getPrimaryPrincipal();//得到唯一的安全数据//2.根据用户数据获取用户的权限信息(所有角色,所有权限)Set<String> roles = new HashSet<>();//所有角色Set<String> perms = new HashSet<>();//所有权限for (Role role : user.getRoles()) {roles.add(role.getName());for (Permission perm : role.getPermissions()) {perms.add(perm.getCode());}}SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();info.setStringPermissions(perms);info.setRoles(roles);return info;}/*** 认证方法* 参数:传递的用户名密码*/protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {//1.获取登录的用户名密码(token)UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;String username = upToken.getUsername();//用户录入的账号//2.根据用户名查询数据库//MyBatis情景下:user对象中包含ID,name,pwd(匿名)//JPA情景下:user对象中包含ID,name,pwd(匿名),set<角色>,set<权限>User user = userService.findByName(username);//3.判断用户是否存在或者密码是否一致if (user != null) {//4.如果一致返回安全数据//构造方法:安全数据,密码(匿名),混淆字符串(salt),realm域名SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), this.getName());return info;}//5.不一致,返回null(抛出异常)return null;}/*** @param* @return bean标签 init-method属性* @Description 自定义密码比较器*/@PostConstructpublic void initCredentialsMatcher() {//指定密码算法HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(DigestsUtil.SHA1);//指定迭代次数hashedCredentialsMatcher.setHashIterations(DigestsUtil.COUNTS);//生效密码比较器setCredentialsMatcher(hashedCredentialsMatcher);}}为需要权限的接口加上注解@PostMApping("/findPage")@RequiresPermissions("group-find")//需要group-find权限访问public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {PageResult pageResult = checkGroupService.pageQuery(queryPageBean);return pageResult;}
推荐阅读
- 光猫与无线路由器的连接方式 猫接路由器
- 家长如何与初中生进行有效沟通 家长如何与初中生沟通
- 地下城与勇士|DNF:韩服110级军团版本职业榜!幻神女气功逆袭,超越奶妈成第三
- 秦昊|秦昊与美女饭局搞暧昧,两人双手紧握相聊甚欢,伊能静到场秒认怂
- 唐宁|唐宁:张国荣“妹妹”,与大13岁老公离婚后,竟落魄到去扫厕所?
- 如何教育青春期男孩,如何教育青春期男孩与女生的交往
- 水星|吴士宏:越过山丘,打破人生与事业的迷障
- 解决学生常见厌学心理的对策 青少年厌学心理问题与对策
- 从姓氏看文化渊源 姓氏源流与文化寻根
- 扈三娘与王矮虎?扈三娘和王英结婚是哪几
