Shiro访问数据库完成用户授权

版权声明:本文为博主原创文章,版权归原作者小思所有,转载或者引用本文内容请注明来源及原作者,https://blog.csdn.net/zeal9s/ https://blog.csdn.net/zeal9s/article/details/83384929

完整项目代码:https://download.csdn.net/download/zeal9s/10744751

1.新建Maven项目Shiro_01,并设置项目输出文件显示

2.访问apache官网下载地址搜索到shiro,下载shiro的core、web、整合spring的依赖并且导入到项目中的pom.xml
在这里插入图片描述
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Shiro_02</groupId>
  <artifactId>Shiro_02</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Shiro_02 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
   <dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!--引入servlet依赖:解决jsp页面报错 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.0-b07</version>
			<scope>provided</scope>
		</dependency>
		<!--引入shiro的核心依赖 -->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-core</artifactId>
			<version>1.3.2</version>
		</dependency>
		<!--引入shiro的web依赖 -->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-web</artifactId>
			<version>1.3.2</version>
		</dependency>
		<!--引入shiro整合spring -->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-spring</artifactId>
			<version>1.3.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
  </dependencies>
  <build>
    <finalName>Shiro_02</finalName>
  </build>
</project>

3.项目的模块图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20181025191820752.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw==,size_27,color_FFFFFF,t_70

4.新建com.zs.shiro包和MyRealm类
MyRealm.java

package com.zs.shiro;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class MyRealm extends AuthorizingRealm {

	// 授权
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		// 用户名
		String uname = (String) principals.getPrimaryPrincipal();
		//模拟通过用户名到数据库查询用户所拥有的角色
		List<String> roles=new ArrayList<String>();
		roles.add("father");
		//模拟通过用户名到数据库查询用户所拥有的角色,角色所拥有的权限
		List<String> behaves=new ArrayList<String>();
		behaves.add("add");
		behaves.add("delete");
		behaves.add("update");
		behaves.add("query");
		//授权信息
		SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
		//将角色赋予用户
		info.addRoles(roles);
		//将权限赋予角色
		info.addStringPermissions(behaves);
		//将授权信息返回测试类TestShiro.java
		return info;
	}

	// 认证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// 创建保存用户名和密码的令牌
		UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
		// 从令牌中获取用户名
		String uname = usernamePasswordToken.getUsername();
		// 模拟通过用户名从数据库查询密码
		String dbpwd = "123";
		// 认证
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(uname, dbpwd, getName());
		return info;
	}

}

5.新建shiro-realm.ini
shiro-realm.ini

myrealm=com.zs.shiro.MyRealm
securityManager.realm=$myrealm

6.新建com.zs.test包和TestShiro类

package com.zs.test;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

/**
 * 
 * @ClassName: TestShiro
 * @Description:手动写用户配置文件shiro.ini完成授权过程
 * @author 小思
 * @date 2018年10月25日 下午4:14:21
 *
 */
public class TestShiro {
	public static void main(String[] args) {
		// 认证过程之后的使用
		// 创建shiro的认证工厂,指定认证的文件
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm.ini");
		// 通过认证工厂,获取认证管理者
		SecurityManager securityManager = factory.getInstance();
		// 将安全管理者添加到当前的安全管理环境中
		SecurityUtils.setSecurityManager(securityManager);
		// 获取安全管理的项目
		Subject subject = SecurityUtils.getSubject();
		// 用户输入的用户名和密码
		String uname = "admin";
		String pwd = "123";
		// 创建保存用户的用户名和密码的令牌
		UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(uname, pwd);
		try {
			// 登录认证
			subject.login(usernamePasswordToken);
			// 认证结果
			boolean result = subject.isAuthenticated();
			System.out.println("认证结果" + result);
		} catch (Exception e) {
		}

		// 授权过程之后的使用
		// 判断用户是否具有某个角色
		boolean role = subject.hasRole("father");
		System.out.println("用户具有father角色吗?" + role);
		// 角色是否拥有某个权限
		boolean behave=subject.isPermitted("update");
		System.out.println("father具有update权限吗?" + behave);
		// 角色是否拥有某个权限
		boolean behaves=subject.isPermittedAll("update","query");
		System.out.println("father具有update、query权限吗?" + behaves);

	}

}

7.授权结果
(1)授权成功:用户输入的用户名和通过用户名查询的权限和角色设置到用户,测试类查询有该权限
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(2)授权失败:用户输入的用户名和通过用户名查询的权限和角色设置到用户,测试类查询没有该权限
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

说在最后的话:编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~

猜你喜欢

转载自blog.csdn.net/zeal9s/article/details/83384929
今日推荐