首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Shiro——基于java的安全框架

Shiro——基于java的安全框架

作者头像
时间静止不是简史
发布2020-07-27 10:37:29
1.5K0
发布2020-07-27 10:37:29
举报
文章被收录于专栏:Java探索之路Java探索之路

一、介绍

简介

Apache Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和 会话管理等功能。 对于任何一个应用程序,Shiro都可以提供全面的安全管理服务。其不仅可 以用在JavaSE环境,也可以用在JavaEE环境。

使用情景

在这里插入图片描述
在这里插入图片描述

功能结构

结构图

在这里插入图片描述
在这里插入图片描述

结构说明

Shiro 把 Shiro 开发团队称为“应用程序的四大基石”——身份验证,授权,会话管理和加密作为其目标。

  • Authentication:有时也简称为“登录”,这是一个证明用户是他们所说的他们是谁的行为。
  • Authorization:访问控制的过程,也就是绝对“谁”去访问“什么”。
  • Session Management:管理用户特定的会话,即使在非 Web 或 EJB 应用程序。
  • Cryptography:通过使用加密算法保持数据安全同时易于使用。

附加功能: 支持和加强在不同环境下所关注的方面

在这里插入图片描述
在这里插入图片描述

外部架构

结构图

在这里插入图片描述
在这里插入图片描述

结构说明

应用程序代码想通过安全管理首先要通过主体(Subject,代表当前用户),主体想通过认证需要调用Shiro安全管理器(Shiro SecurityManager,管理所有主体)。想要进行认证和比对就需要使用Realm(域,提供给你安全数据)获取其中的权限。

总结: 1、应用代码通过Subject来进行认证和授权,而Subject又委托给SecurityManager; 2、我们需要给Shiro的SecurityManager注入Realm,从而让SecurityManager能得到合法的用户及其权限进行判断。

二、配置文件

应用场景

适用于用户少且不需要在运行时动态常见的情景下使用

介绍

在没有使用spring框架之前时,一般会在src目录下创建一个shiro.ini的文件, 对其进行配置 ; 在使用srpingmvc整合后,需要在spring的配置文件中添加spring-shiro.xml(名字可以随便起, 但是要尽量做到见名知意) 1、对象名=全限定类名 相对于调用public无参构造器创建对象 2、对象名.属性名=值 相当于调用setter方法设置常量值 3、对象名.属性名=$对象引用 相当于调用setter方法设置对象引用

详情见: https://jinnianshilongnian.iteye.com/blog/2020820

三、基于shiro的相关案例

常用词汇

Subject :主体 Security: 安全 Realm : 域,范围 Authenticator : 认证器 Authentication : 认证 Authorizer : 授权器 Cryptography : 密码,加密 Credential : 证书,凭证 Matcher : 匹配器 Principal : 身份

相关jar包

maven中的坐标

<dependencies> 
	<dependency> 
		<groupId>org.apache.shiro</groupId> 
		<artifactId>shiro-core</artifactId> 
		<version>1.3.2</version> 
	</dependency> 
	<!-- Shiro use SLF4J for logging. We'll use the 'simple' binding         in this example app. See http://ww.slf4j.org for more info. -->
	 <dependency> 
		 <groupId>org.slf4j</groupId> 
		 <artifactId>slf4j-simple</artifactId>
		  <version>1.7.5</version> 
		  <scope>test</scope> 
	  </dependency> 
  <dependencies> 

身份认证

实现最简单的身份认证,后续还会修改

身份与凭证

身份就是用来标识主题的属性,唯一即可.可以是身份证号或手机号等 凭证就是只要当前用户(主体)知道的安全知识,如密码或数字签名证书等

步骤

  1. 导入jar包
  2. 从源码的示例项目quickstart中拷贝shiro.ini放到src下,并 配置
  3. 编写代码

所需要的jar

在这里插入图片描述
在这里插入图片描述

shiro.ini

Shiro.ini
[users]
victor=123456
//实现简单认证
public class AuthenticationTest {
	@Test
	public void testAuthentication(){
		//1.构建SecurityManager工厂
		IniSecurityManagerFactory securityManagerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");
		//2.通过securityManagerFactory工厂获取SecurityManager实例
		SecurityManager securityManager = securityManagerFactory.getInstance();
		//3.将securityManager设置到运行环境当中
		SecurityUtils.setSecurityManager(securityManager);
		//4.获取subject实例
		Subject subject = SecurityUtils.getSubject();
		//5.创建用户名密码验证令牌Token
		UsernamePasswordToken token = new UsernamePasswordToken("victor","123456");
		//6.进行身份验证
		subject.login(token);
		//7.判断是否认证通过
		System.out.println(subject.isAuthenticated());
	}
}

JDBCRealm

Realm 域,Shiro从从Realm获取安全数据(如用户、角色、权限),可以把Realm看成DataSource,即安全数据源。就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法;也需要从Realm得到用户相应的角色/权限进行验证用户是否能进行操作;

步骤:

  1. 建users表(表名、字段对应上)表名必须是username,用户名必须是username, 密码是password
  2. 添加jar包(数据库驱动、数据库连接池、beanutils等)
  3. 编写shiro.ini
  4. 编写测试代码

jar包

jar包
jar包
创建安全设置文件 shiro.ini(注意大小写和美元符)
		[main]
		#配置Realm
		jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
		#配置数据源
		dataSource = com.mchange.v2.c3p0.ComboPooledDataSource
		dataSource.driverClass = com.mysql.jdbc.Driver
		dataSource.jdbcUrl= jdbc:mysql:///test
		dataSource.user = root
		dataSource.password = root
		jdbcRealm.dataSource = $dataSource
		
		#将Realm注入给securityManager
		securityManager.realm = $jdbcRealm
//实现简单认证
public class AuthenticationTest {
	@Test
	public void testAuthentication(){
		//1.构建SecurityManager工厂
		IniSecurityManagerFactory securityManagerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");
		//2.通过securityManagerFactory工厂获取SecurityManager实例
		SecurityManager securityManager = securityManagerFactory.getInstance();
		//3.将securityManager设置到运行环境当中
		SecurityUtils.setSecurityManager(securityManager);
		//4.获取subject实例
		Subject subject = SecurityUtils.getSubject();
		//5.创建用户名密码验证令牌Token
		UsernamePasswordToken token = new UsernamePasswordToken("victor","123456");
		//6.进行身份验证
		subject.login(token);
		//7.判断是否认证通过
		System.out.println(subject.isAuthenticated());
	}
}

自定义Realm

0、创建数据库并插入数据
1、导包
在这里插入图片描述
在这里插入图片描述

2、创建CustomRealm类,继承AuthenticatingRealm, 负责从自定义的数据库中查询账号密码

public class CustomRealm extends AuthenticatingRealm{

	private  String principal;
	private  String credentials;

	//注意连接源码
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		
		//jdbc查询操作
		//创建jdbc变量
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		//创建变量
		try {
			//创建连接
			conn=DBUtil.getConnection();
			//创建sql语句
			String sql="select username,passwd from starLogin";
			//创建sql预处理命令
			ps=conn.prepareStatement(sql);
			//给占位符赋值
			//执行sql语句
			rs=ps.executeQuery();
			//遍历
			while (rs.next()) {
				principal=rs.getString("username");
				credentials=rs.getString("passwd");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//关闭连接
			DBUtil.closeAll(rs, ps, conn);
		}
	
		SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal, credentials, "customRealm");
		return simpleAuthenticationInfo;
	}
}

注: SimpleAuthenticationInfo构造器中的三个参数的含义

SimpleAuthenticationInfo(Object principal, Object credentials, String realmName)
Principal:身份 (用户名)  
credentials:凭证(密码)  
realmName:域名称(自定义realm的名称)

3、创建安全设置文件 shiro.ini(注意首字母小写和美元符)

			[main]
			#配置Realm
			customRealm = com.bjsxt.Realms.CustomRealm
			
			#将Realm注入给securityManager
			securityManager.realm = $customRealm

4、创建测试类

//实现简单认证
public class AuthenticationTest {
	@Test
	public void testAuthentication(){
		//1.构建SecurityManager工厂
		IniSecurityManagerFactory securityManagerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");
		//2.通过securityManagerFactory工厂获取SecurityManager实例
		SecurityManager securityManager = securityManagerFactory.getInstance();
		//3.将securityManager设置到运行环境当中
		SecurityUtils.setSecurityManager(securityManager);
		//4.获取subject实例
		Subject subject = SecurityUtils.getSubject();
		//5.创建用户名密码验证令牌Token
		UsernamePasswordToken token = new UsernamePasswordToken("victor","123");
		//6.进行身份验证
		subject.login(token);
		//7.判断是否认证通过
		System.out.println(subject.isAuthenticated());
	}
}

MD5加密

加密算法的分类

对称加密算法 非对称加密算法 散列算符

测试代码

//MD5加密、加盐以及迭代
public class MD5Test {
	@Test
	public void testMD5(){
		//md5加密
		Md5Hash md5 = new Md5Hash("123456");
		System.out.println(md5);
		//加盐
		md5 = new Md5Hash("123456", "bjsxt");
		System.out.println(md5);
		//迭代
		md5 = new Md5Hash("123456", "bjsxt", 2);
		System.out.println(md5);
	}
}

加盐与迭代

加盐

原理: 给原文加入随机数生成新的MD5值。 使用MD5存在一个问题,相同的password生产的Hash值是相同的,如果两个用户设置了相同的密码,那么数据库当就会存储相同的值,这样是极不安全的。 加Salt可以一定程度上解决这一问题。其基本想法是:当用户首次提供密码时(通常是注册时), 由系统自动往这个密码里撒一些“佐料”,然后再散列。而当用户登录时, 系统为用户提供的代码撒上同样的“佐料”,然后散列,再比较散列值,来确定密码是否正确。

迭代

进行加密的次数

测试代码

//MD5加密、加盐以及迭代
public class MD5Test {
	
	public void testMD5(){
		//md5加密
		Md5Hash md5 = new Md5Hash("123456");
		System.out.println(md5);
		//加盐
		md5 = new Md5Hash("123456", "bjsxt");
		System.out.println(md5);
		//迭代
		md5 = new Md5Hash("123456", "bjsxt", 2);
		System.out.println(md5);
	}
}

凭证匹配器

在Realm接口的实现类AuthenticatingRealm中有credentialsMatcher属性。 意为凭证匹配器。常用来设置加密算法及迭代次数等。

授权

授权,又称作为访问控制,是对资源的访问管理的过程。即对于 认证通过的用户,授予他可以访问某些资源的权限。 授权方式分为: 代码触发、注解触发、标签触发

测试代码

自定义realm
public class CustomRealm extends AuthorizingRealm {

	private String principal;
	private String credentials;
	private ResultSet rs;
	private Statement state;
	private Connection conn;
	private String roleName;
	private String remark;

	// 认证方法:获取认证信息
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// 使用JDBC,从数据库获取数据
		try {
			// 1.注册驱动
			Driver driver = new Driver();
			DriverManager.registerDriver(driver);
			// 2.获取连接对象
			String url = "jdbc:mysql:///test";
			String user = "root";
			String password = "root";
			conn = DriverManager.getConnection(url, user, password);
			state = conn.createStatement();
			// 4.执行sql语句
			String sql = "select username,password from users";
			rs = state.executeQuery(sql);
			// 5.处理结果集
			if(rs.first()){
				principal = rs.getString("username");
				credentials = rs.getString("password");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal, credentials,
				"customRealm");
		return simpleAuthenticationInfo;
	}

	// 授权方法:获取授权信息
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		// 使用JDBC,从数据库获取数据
		try {
			// 1.注册驱动
			Driver driver = new Driver();
			DriverManager.registerDriver(driver);
			// 2.获取连接对象
			String url = "jdbc:mysql:///test";
			String user = "root";
			String password = "root";
			conn = DriverManager.getConnection(url, user, password);
			state = conn.createStatement();
			// 4.执行sql语句
			//String sql = "select name from role";
			String sql = "select remark from permission";
			rs = state.executeQuery(sql);
			// 5.处理结果集
			if(rs.first()){
				remark = rs.getString("remark");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (state != null) {
				try {
					state.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		//info.addRole(roleName);
		info.addStringPermission(remark);
		return info;
	}

}
配置文件shiro.ini
[main]
#配置Realm
customRealm = com.bjsxt.realms.CustomRealm

#将Realm注入给SecurityManager
securityManager.realm = $customRealm
//测试类 : 实现简单认证
public class AuthorizationTest {
	@Test
	public void testAuthorization(){
		//1.构建SecurityManager工厂
		IniSecurityManagerFactory securityManagerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");
		//2.通过securityManagerFactory工厂获取SecurityManager实例
		SecurityManager securityManager = securityManagerFactory.getInstance();
		//3.将securityManager设置到运行环境当中
		SecurityUtils.setSecurityManager(securityManager);
		//4.获取subject实例
		Subject subject = SecurityUtils.getSubject();
		//5.创建用户名密码验证令牌Token
		UsernamePasswordToken token = new UsernamePasswordToken("zhangsan","123456");
		//6.进行身份验证
		subject.login(token);
		//7.判断是否认证通过
		System.out.println(subject.isAuthenticated());
		
		//认证通过后进行授权:代码触发
		//基于角色授权
		//boolean hasRole = subject.hasRole("普通员工");
		//System.out.println(hasRole);
		//基于权限授权
		boolean permitted = subject.isPermitted("一级菜单,基本设置操作权限");
		System.out.println(permitted);
	}
}

四、Shiro-与Spring整合实现登录认证

步骤

1.导入rbac项目 2.导入shiro相关jar包(shiro-all 以及shiro-spring) 3.在web.xml中添加DelegatingFilterProxy配置 4.编写spring-shiro.xml 5.编写mapper接口及mapper.xml 6.编写service接口及实现类 7.编写controller 8.编写Realm

web.xml

<!-- 注册DelegatingFilterProxy:通过代理模式将servlet容器中的filter同Spring容器中的bean关联起来 -->
  <filter>
  	<filter-name>shiro</filter-name>
  	<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  	<init-param>
  		<!-- 该属性为true表明启用引入filter的init()和destroy(),也就是spring容器中对应的filter生命周期交给servlet容器管理 -->
  		<param-name>targetFilterLifecycle</param-name>
  		<param-value>true</param-value>
  	</init-param>
  	<init-param>
  		<!-- 该属性设置Spring容器中filter的bean的id -->
  		<param-name>targetBeanName</param-name>
  		<param-value>shiroFilter</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>shiro</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

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"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 注册凭证匹配器-->
    <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    	<property name="hashAlgorithmName" value="md5"></property>
    	<property name="hashIterations" value="2"></property>
    </bean>
    <!-- 注册自定义Realm -->
    <bean id="customRealm" class="com.bjsxt.realms.CustomRealm">
    	<property name="credentialsMatcher" ref="credentialsMatcher"></property>
    </bean>
    <!-- 注册SecurityManager -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    	<property name="realms" ref="customRealm"></property>
    </bean>
    <!-- 注册ShiroFilterFactoryBean 注意:id名称必须与web.xml中过滤器名称对应 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    	<property name="securityManager" ref="securityManager"></property>
    	<property name="loginUrl" value="/user/login.do"></property>
    	<property name="successUrl" value="/jsp/users.jsp"></property>
    	<property name="unauthorizedUrl" value="/jsp/refuse.jsp"></property>
    	<!-- 设置过滤器链属性 -->
    	<property name="filterChainDefinitions">
    		<value>
    			/user/login.do=authc
    			/**=anon
    		</value>
    	</property>
    </bean>
</beans>

mapper接口及mapper.xml

package com.bjsxt.rbac.mapper;

import com.bjsxt.rbac.pojo.Users;

public interface IUserMapper {
	//根据用户名查询用户信息
	Users selectByUsername(String username);

}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.rbac.mapper.IUserMapper">
	
	<select id="selectByUsername" resultType="Users">
		select
		*
		from users
		where username=#{uname}
	</select>
</mapper>

service接口及实现类

package com.bjsxt.rbac.service;

import com.bjsxt.rbac.pojo.Users;

public interface IUserService {
	//根据用户名查询用户信息
	Users selectByUsername(String username);
}

```java

package com.bjsxt.rbac.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bjsxt.rbac.mapper.IUserMapper;
import com.bjsxt.rbac.pojo.Users;
import com.bjsxt.rbac.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {
	@Resource
	private IUserMapper mapper;

	//根据用户名查询用户信息
	@Override
	public Users selectByUsername(String username) {
		return mapper.selectByUsername(username);
	}
	
	

}

controller

@Controller
@Scope("prototype")
@RequestMapping("/user")
public class UserController {
	
	
	/**
	 * 设定登录失败跳转的资源以及获取异常信息
	 */
	@RequestMapping("/login.do")
	public String login(HttpServletRequest request,Model model){
		//查看具体异常信息,获取异常信息名
		Object ex = request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
		if(UnknownAccountException.class.getName().equals(ex)){
			model.addAttribute("msg", "账户不正确");
		}else if(IncorrectCredentialsException.class.getName().equals(ex)){
			model.addAttribute("msg", "凭证不正确");
		}else{
			System.out.println(ex);
			model.addAttribute("msg", "未知验证异常");
		}
		return "/jsp/exception.jsp";
	}	
}

编写Realm

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.bjsxt.rbac.pojo.Users;
import com.bjsxt.rbac.service.IUserService;

public class CustomRealm extends AuthorizingRealm {

	
	@Autowired
	private IUserService userService;
	// 认证方法:获取认证信息
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
		
		try {
			//调用service
			//根据表单传过来的用户名查询用户信息
			Users user = userService.selectByUsername(token.getPrincipal().toString());
			System.out.println(user);
			if (user != null) {
				ByteSource newSalt = ByteSource.Util.bytes(user.getPassword_salt());
				SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(token.getPrincipal(),
						user.getPassword(), newSalt, token.getPrincipal().toString());
				return simpleAuthenticationInfo;
			} else {
				return null;
			} 
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 授权方法:获取授权信息
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		return null;
	}

}

五、remberMe(七天免登陆实现)

步骤

  1. 用户类及其引用类全部实现序列化‘
  2. 修改登陆页面
  3. 修改Spring-shiro.xml

代码实现

用于登陆的jsp页面,添加一个单选框

<input type="checkbox" name="rememberMe" value="true"/>记住我<br>
spring-shiro.xml
(标注rem的都是需要添加的)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 注册凭证匹配器-->
    <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    	<property name="hashAlgorithmName" value="md5"></property>
    	<property name="hashIterations" value="2"></property>
    </bean>
    <!-- 注册自定义Realm -->
    <bean id="customRealm" class="com.bjsxt.realms.CustomRealm">
    	<property name="credentialsMatcher" ref="credentialsMatcher"></property>
    </bean>
    <!-- 注册SessionManager -->
    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
    	<property name="globalSessionTimeout" value="300000"></property>
    	<property name="deleteInvalidSessions" value="true"></property>
    </bean>
    
    <!-- rem: 注册SimpleCookie -->
    <bean id="simpleCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
    	<!-- 设置Cookie名称 -->
    	<property name="name" value="rememberMe"></property>
    	<!-- 设置Cookie有效时间  单位为秒-->
    	<property name="maxAge" value="604800"></property>
    </bean>
    <!-- rem: 注册RememberMeManager -->
    <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
    	<property name="cookie" ref="simpleCookie"></property>
    </bean>
    <!-- 注册SecurityManager -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    	<property name="realms" ref="customRealm"></property>
    	<property name="sessionManager" ref="sessionManager"></property>
    	<property name="rememberMeManager" ref="rememberMeManager"></property>
    </bean>
    <!-- 注册ShiroFilterFactoryBean 注意:id名称必须与web.xml中过滤器名称对应 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    	<property name="securityManager" ref="securityManager"></property>
    	<property name="loginUrl" value="/user/login.do"></property>
    	<property name="successUrl" value="/user/loginSuccess.do"></property>
    	<property name="unauthorizedUrl" value="/WEB-INF/jsp/refuse.jsp"></property>
    	<!--rem(/user/loginSuccess.do=user
    			/menu/showMenu.do=user) : 设置过滤器链属性 -->
    	<property name="filterChainDefinitions">
    		<value>
    			/user/login.do=authc
    			/**=anon
    			/user/loginSuccess.do=user
    			/menu/showMenu.do=user
    		</value>
    	</property>
    </bean>
</beans>
web.xml中注册
 <!-- 注册DelegatingFilterProxy:通过代理模式将servlet容器中的filter同Spring容器中的bean关联起来 -->
  <filter>
  	<filter-name>shiro</filter-name>
  	<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  	<init-param>
  		<!-- 该属性为true表明启用引入filter的init()和destroy(),也就是spring容器中对应的filter生命周期交给servlet容器管理 -->
  		<param-name>targetFilterLifecycle</param-name>
  		<param-value>true</param-value>
  	</init-param>
  	<init-param>
  		<!-- 该属性设置Spring容器中filter的bean的id -->
  		<param-name>targetBeanName</param-name>
  		<param-value>shiroFilter</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>shiro</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、介绍
  • 简介
  • 使用情景
  • 功能结构
    • 结构图
      • 结构说明
      • 外部架构
        • 结构图
          • 结构说明
          • 二、配置文件
            • 应用场景
              • 介绍
              • 三、基于shiro的相关案例
                • 常用词汇
                  • 相关jar包
                    • maven中的坐标
                  • 身份认证
                    • 身份与凭证
                    • 步骤
                  • JDBCRealm
                    • 步骤:
                      • 自定义Realm
                        • MD5加密
                          • 加密算法的分类
                          • 测试代码
                        • 加盐与迭代
                          • 加盐
                          • 迭代
                          • 测试代码
                        • 凭证匹配器
                          • 授权
                            • 测试代码
                            • 四、Shiro-与Spring整合实现登录认证
                              • 步骤
                                • web.xml
                                  • spring-shiro.xml
                                    • mapper接口及mapper.xml
                                      • service接口及实现类
                                        • controller
                                          • 编写Realm
                                          • 五、remberMe(七天免登陆实现)
                                            • 步骤
                                              • 代码实现
                                              相关产品与服务
                                              数据库
                                              云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
                                              领券
                                              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档