Springboot之Shiro配置

Shiro安全认证实战
本文详细介绍了如何在SpringBoot项目中使用Shiro进行安全认证,包括导入核心包、配置类、编写Realm类、实现登录及权限显示。通过具体步骤和代码示例,展示了Shiro在权限管理和用户认证方面的应用。

Shiro 安全认证 (基于idea、maven、springboot)

1.导入jar

这个包是shiro的核心包
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.4.0</version>
</dependency>
这个jar是页面设置有权限才显示的jar
<dependency>
    <groupId>com.github.theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.0.0</version>
</dependency>

2.配置类

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;

@Configuration
public class ShiorConfig {
    //shiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);
        //添加shiro的内置过滤器
        /*anon:无需认证就可以访问
        authc 必须认证才能访问
        user 必须拥有记住我的功能才用
        perms 拥有某个资源的权限才能访问
        role拥有某个角色权限才能访问
        * */
        LinkedHashMap<String, String> filtermap = new LinkedHashMap<>();
        filtermap.put("/TA/toadd","perms[user:add]");//表示只有user:add字段的才能查看
        filtermap.put("/TA/toupdata","perms[user:updata]");//表示只要登陆授权了,就可以查看

        bean.setFilterChainDefinitionMap(filtermap);
        bean.setLoginUrl("/tologin");//表示没有登陆就跳回去
        /*为授权页面*/
        bean.setUnauthorizedUrl("/not");//表示没有授权的
        return bean;
    }

    //DafaultWebSecurityManaage:2
    @Bean(name="securityManager")
    public DefaultWebSecurityManager getdefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return  securityManager;
    }
    //创建realm对象,需要自定义类:1
    @Bean(name="userRealm")
    public UserRealm userRealm(){
        return new UserRealm();
    }
    
    /*在页面显示中是否有权限,有就显示*/
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }
}
3.编写Realm类
package com.taxing.mb.my.config;
import com.taxing.mb.my.mapper.UserMapper;
import com.taxing.mb.my.pojo.UserP;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
public class UserRealm extends AuthorizingRealm {
    @Autowired
    UserMapper userMapper;
   //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Subject subject = SecurityUtils.getSubject();
        UserP up = (UserP) subject.getPrincipal();//获得认证方法中传递的参数
        String[] rm = up.getRealm().split(";");
        for (int i=0;i<rm.length;i++){
            System.out.println("用户权限:"+rm[i]);
            info.addStringPermission( rm[i]);//添加授权
        }
        return info;
    }
    /*认证*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;//次参数可得到 Subject subject = SecurityUtils.getSubject();对象中存的值
        UserP user = userMapper.getUserByName(userToken.getUsername());//用户名验证
      if (user==null){
            return  null;
        }
      /*密码shiro自动做*/
//参数1可以把参数传递给授权方法中,参数2为shiro给我的做密码判断
       return new SimpleAuthenticationInfo(user,user.getPassword(),"");
    }
}
4.进行登陆
/*
* 获得登陆信息
* */
@RequestMapping("/tohome")
public String tohome(String name,String password){
    Subject subject = SecurityUtils.getSubject();
   UsernamePasswordToken token = new UsernamePasswordToken(name, password);
    try{
       subject.login(token);//执行登陆方法,如果没有异常就ok
        return "home";
    }catch (UnknownAccountException e){
        /*名字错误*/
        return "index";
    }catch (IncorrectCredentialsException e){
        /*密码错误*/
        return  "index";
    }
}

5.设置页面有权限的显示

<!DOCTYPE html>
<html lang="en"
      xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">//注意要这个命名空间
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
//表示有这个权限才显示,否则不显示
<div shiro:hasPermission="user:add">
    <a href="/TA/toadd">添加</a>
</div>

<div shiro:hasPermission="user:updata" >
    <a href="/TA/toupdata">更新页面</a>
</div>
</body>
</html>
Spring Boot项目中使用Shiro进行安全控制,需要进行以下几个步骤。 1. 引入ShiroSpring Boot相关依赖 在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.6.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.6.0</version> </dependency> ``` 2. 配置Shiro安全管理器 在Spring Boot配置文件中,添加Shiro的相关配置: ```yaml # Shiro配置 shiro: # 配置安全管理器 securityManager: # 配置Realm realms: - name: myRealm # 配置自定义的Realm实现类 customRealm: com.example.demo.shiro.MyRealm # 配置Session管理器 sessionManager: # Session过期时间,单位:毫秒 globalSessionTimeout: 1800000 # 配置Cookie cookie: # Cookie名称 name: SHIROSESSIONID # Cookie过期时间,单位:秒 maxAge: 1800 # Cookie路径 path: / # 配置Shiro Filter filters: # 配置自定义的Filter实现类 myFilter: com.example.demo.shiro.MyFilter # 配置Filter Chain filterChainDefinitions: # 配置访问控制规则 /login: anon /logout: logout /**: myFilter ``` 其中,配置安全管理器`securityManager`,包括了自定义的Realm实现类`MyRealm`、Session管理器和Cookie。 配置Shiro Filter`myFilter`,以及Filter Chain,其中`/login`可以匿名访问,`/logout`需要进行登出操作,其他URL需要经过自定义的Filter`MyFilter`进行访问控制。 3. 实现自定义Realm 在`MyRealm`类中,需要实现Shiro的`Realm`接口,重写`doGetAuthenticationInfo`和`doGetAuthorizationInfo`方法,完成身份认证和授权。 ```java public class MyRealm extends AuthorizingRealm { /** * 身份认证 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { // 获取用户名和密码 String username = (String) authenticationToken.getPrincipal(); String password = new String((char[]) authenticationToken.getCredentials()); // 根据用户名查询用户信息 User user = userService.findByUsername(username); // 判断用户是否存在 if (user == null) { throw new UnknownAccountException("用户不存在"); } // 判断密码是否正确 if (!password.equals(user.getPassword())) { throw new IncorrectCredentialsException("密码错误"); } // 将用户信息封装到AuthenticationInfo中 SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, password, getName()); return authenticationInfo; } /** * 授权 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { // 获取用户信息 User user = (User) principalCollection.getPrimaryPrincipal(); // 查询用户角色和权限信息 List<Role> roles = roleService.findByUserId(user.getId()); List<Permission> permissions = permissionService.findByUserId(user.getId()); // 封装角色和权限信息到AuthorizationInfo中 SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); for (Role role : roles) { authorizationInfo.addRole(role.getName()); } for (Permission permission : permissions) { authorizationInfo.addStringPermission(permission.getName()); } return authorizationInfo; } } ``` 4. 实现自定义Filter 在`MyFilter`类中,需要继承Shiro的`AccessControlFilter`类,重写`isAccessAllowed`和`onAccessDenied`方法,完成访问控制逻辑。 ```java public class MyFilter extends AccessControlFilter { @Override protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception { // 获取Shiro的Subject对象 Subject subject = getSubject(servletRequest, servletResponse); // 判断是否已经登录 if (subject.isAuthenticated()) { return true; } // 判断是否是登录请求 HttpServletRequest request = (HttpServletRequest) servletRequest; if ("/login".equals(request.getServletPath())) { return true; } // 其他情况都要进行跳转到登录页面 redirectToLogin(servletRequest, servletResponse); return false; } @Override protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception { // 如果是Ajax请求,则返回JSON格式的数据 HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; if (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").equalsIgnoreCase("XMLHttpRequest")) { response.setContentType("application/json;charset=UTF-8"); response.getWriter().write(JSON.toJSONString(Result.fail("未登录或登录超时"))); } else { // 否则进行跳转到登录页面 redirectToLogin(servletRequest, servletResponse); } return false; } } ``` 5. 配置Shiro的注解支持 在Spring Boot配置类中,添加`@EnableAspectJAutoProxy(proxyTargetClass = true)`注解和`@EnableAuthorization`注解,启用Shiro的注解支持和AOP功能。 ```java @Configuration @EnableAspectJAutoProxy(proxyTargetClass = true) @EnableAuthorization public class ShiroConfig { // ... } ``` 至此,基于Shiro安全控制已经配置完成。在需要进行访问控制的Controller方法上,添加相应的注解即可完成授权操作,例如`@RequiresPermissions("user:view")`表示需要拥有`user:view`权限才能访问该方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值