Spring-Shiro整合
发布日期:2022-02-01 14:28:19 浏览次数:46 分类:技术文章

本文共 4246 字,大约阅读时间需要 14 分钟。

因为web项目的核心组件(Service,dao)都在spring工厂中管理,利用IOC和AOP,组建了关系松散,稳健的系统。

shiro的诸多组件也需要由spring统一管理,进而可以更好的和其他组件协作,因为shiro中大多数组件都是pojo类,更方便管理我们可以很方便地把它们从shiro.ini中迁到Spring工厂中。
pom文件

org.apache.shiro
shiro-spring
1.4.0

applicationContext_shiro.xml配置文件

将shiro的三大核心组件SecurityMnager,Subject,Realm,放到配置文件,这里我们可以将shiro单独放到一个配置文件中,在Spring的配置文件中导入该配置文件即可。

/user/login = anon /user/login = anon /user/all = authc,roles["admin"] /user/update = authc,roles["manager","seller"] /user/delete = authc, perms["user:update","user:delete"] /user/logout = logout

web.xml

shiroFilter
org.springframework.web.filter.DelegatingFilterProxy
targetFilterLifecycle
true
shiroFilter
/*

另外大家发现了我们在这里也直接整合了Mybatis。因为之前我们放在shiro.ini中的用户名,权限和角色信息都没有了 ,这里我们自定义了pojo类 将权限角色信息存入数据库中,在需要安全数据时候 在自定义的Realm中注入service并调用dao来进行数据库查询即可 贴一下大概结构图 和realm的代码。

大概结构:(将角色权限分别定义pojo,并将数据存在数据库,由realm中调用service,dao来查询)在这里插入图片描述
自定义realm

@Setterpublic class MyRealm extends AuthorizingRealm {    private UserService userService;//注入service  调用其中dao查询方法即可    private  PermissionService permissionService;    private RoleService roleService;    @Override    //用户做授权和权限认定    /**     * 当subject.login()时,shiro会调用Realm的此方法做用户信息的查询,然后做校验     * 职责:通过用户传递来的用户名查询用户表,获得用户信息     * 返回值:将查到的用户信息(用户名+密码)封装在AuthenticationInfo对象中返回     * 异常:如果没有查到用户可抛出用户不存在异常;如果用户被锁定可抛出用户被锁异常;或其它自定义异常.     * @param token     * @return     * @throws AuthenticationException     */    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {        //获取shiro中的用户名        String username = (String) principalCollection.getPrimaryPrincipal();        //根据用户姓名去表中查询用户角色和权限信息        //根据用户名查询角色信息     //   RoleService roleService = ContextLoader.getCurrentWebApplicationContext().getBean("roleServiceImpl", RoleService.class);        Set
roles = roleService.queryAllRolenameByUsername(username); //根据用户名查权限信息 // PermissionService permissionService =ContextLoader.getCurrentWebApplicationContext().getBean("permissionServiceImpl", PermissionService.class); Set
permissions = permissionService.queryAllPermissionByUsername(username); //将用户权限和角色信息 封装到对象中 SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(roles); simpleAuthorizationInfo.setStringPermissions(permissions); return simpleAuthorizationInfo; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { String username = (String) authenticationToken.getPrincipal(); //根据用户名来查询用户身份信息 // UserService userService = (UserService)ContextLoader.getCurrentWebApplicationContext().getBean("userServiceImpl"); //System.out.println("================"+userService); User user = userService.queryUserByUsername(username); if(user==null){ return null; //返回空后续会返回一个用户名不存在异常 由一场解析器处理 } return new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(), ByteSource.Util.bytes(user.getSalt()),getName()); }}

至此 ,Spring集成shiro基本就完成了 后面想起来什么在做总结和大家分享 希望对大家有所帮助。

转载地址:https://blog.csdn.net/shunshizhen120412/article/details/102598373 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:mybatis中#{ }和${ }的区别
下一篇:决定成功十种积极心态,成功有道理

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月17日 14时34分03秒