- 在web项目pom.xml文件中导入jar包
<!--shiro安全框架jar包--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!--导入shiro的核心包--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.2.3</version> </dependency> <!--导入shiro和spring集成的jar包--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.3.2</version> </dependency> <!--导入shiro的web开发支持的jar包--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.3.2</version> </dependency>
2.在web.xml文件中声明shiro拦截权限的过滤器
<!--在web.xml中声明shiro拦截器的过滤器--> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <!--保证该过滤器的生命周期和spring工厂中shiro过滤器对象的生命周期一致--> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> <!--声明该过滤器代理工厂类中的id为什么的shiro过滤器对象--> <init-param> <param-name>targetBeanName</param-name> <param-value>shiroFilter</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
- 在spring中自定义域对象
在realm中创建AuthRealm类
package com.chang.shiro.realm; import com.chang.shiro.entity.Users; import com.chang.shiro.service.UserService; import org.apache.shiro.authc.*; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; /** * * */ public class AuthRealm extends AuthorizingRealm { @Autowired private UserService userService; //获取授权信息 protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { return null; } //获取认证信息 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { //获取用户名 一般在shiro中放置用户身份信息的时候 访用户对象 不放用户字符串 String username = token.getPrincipal().toString(); //有了用户名要根据用户名在数据库中查询用户对象 Users user = userService.getByUsername(username); //判断如果用户对象不存在,抛出异常 if (user==null){ throw new UnknownAccountException("用户名不存在"); } //封装用户的身份对子那个 返回这个身份对象 SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), "authRealm"); return info; } }
- 创建spring-shiro.xml中配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--创建自定义域对象--> <bean id="authRealm" class="com.chang.shiro.realm.AuthRealm"></bean> <!--创建shiro的安全管理器对象--> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!--声明域 在域中读取认证和授权的数据--> <property name="realm" ref="authRealm"></property> </bean> <!--创建shiro的过滤器对象--> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!--要注入安全管理器对象--> <property name="securityManager" ref="securityManager"></property> <!--配置登录请求的路径--> <property name="loginUrl" value="/user/login.do"></property> <!--配置shiro的认证和授权的过滤器--> <property name="filterChainDefinitions"> <value> <!--对静态资源不拦截 anon指匿名访问的过滤器 所有匿名用户都可访问static下面的资源--> /static/*=anon /user/login.do=anon /login.jsp=anon <!--authc指必须经过认证(登陆过后)才能访问的请求 /*代表所有有一个斜杠的请求都要经过认证--> /*=authc /*/*=authc </value> </property> </bean> </beans>
5.创建文件之后在spring主文件applicationContext.xml中引入该文件
<!--引入shiro的主配置文件--> <import resource="classpath:spring-shiro.xml"></import>
- 在控制器中使用shiro去做登录认证
//用户登录 @RequestMapping("/login") public String login(Users user, Model model, HttpSession session){ //获取用户对象主体 Subject subject = SecurityUtils.getSubject(); //封装用户名和密码的认证信息对象 UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(),user.getPassword()); //进行登录认证 try{ subject.login(token); }catch (Exception e){ e.printStackTrace(); System.out.println("用户名或密码错误"); return "redirect:/login.jsp"; } return "index"; }
文章来源: https://blog.csdn.net/weixin_43381137/article/details/92674122