:spring
package com.shi.authentication; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.config.IniSecurityManagerFactory; import org.apache.shiro.util.Factory; import org.junit.Test; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.subject.Subject; /** * 認證測試 * @author SHF * */ public class AuthenticationTest { //用戶登陸和退出 @Test public void testLoginAndLogout(){ //1 建立securityManager工廠,經過ini配置文件建立securityManage工廠 Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro-first.ini"); //2 建立SecurityManager SecurityManager securityManager=factory.getInstance(); //3 將SecurityManager設置當前的運行環境中 SecurityUtils.setSecurityManager(securityManager); //4 從SecurityUtils裏邊建立一個subject Subject subject=SecurityUtils.getSubject(); //5 在認證提交前準備token(令牌) UsernamePasswordToken token =new UsernamePasswordToken("zhangsan", "111111"); try { //6 執行認證提交 subject.login(token); } catch (Exception e) { e.printStackTrace(); } //是否定證經過 boolean isAuthenticated=subject.isAuthenticated(); System.out.println("是否定證經過:"+isAuthenticated); subject.logout(); //是否定證經過 boolean isAuthenticated2=subject.isAuthenticated(); System.out.println("是否定證經過:"+isAuthenticated2); } }
shiro-first.ini數據庫
#對用戶信息進行設置 [users] #設置用戶名和密碼 zhangsan=111111 lisi=222222
jar包:apache
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.shi.shiro</groupId> <artifactId>Shiro-First</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> </dependencies> </project>
package com.shi.realm; 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.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; public class CustomRealm extends AuthorizingRealm{ //設置realm的名字 @Override public void setName(String name) { super.setName("customRealm"); } /** * 用於認證 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { //1 從token中取出身份信息(token是用戶輸入的) String userCode=(String) token.getPrincipal(); //2 根據用戶輸入的userCode從數據庫查詢 //... 模擬數據庫中取出的密碼是"111111" String password="111111"; //3 若是 查詢不到返回null if(!"zhangsan".equals(userCode)){ return null; } //若是查詢到 返回認證信息AuthenticationInfo SimpleAuthenticationInfo simpleAuthenticationInfo=new SimpleAuthenticationInfo(userCode, password, this.getName()); return simpleAuthenticationInfo; } /** * 用於受權 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { // TODO Auto-generated method stub return null; } }
shiro-realm.ini文件 的配置maven
[main] #自定義realm customRealm=com.shi.realm.CustomRealm #講realm設置到securityManager中,至關於spring中的注入 securityManager.realm=$customRealm
測試代碼:ide
// 2 自定義CustomRealm測試 @Test public void testCustomRealm(){ //1 建立securityManager工廠,經過ini配置文件建立securityManage工廠 Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro-realm.ini"); //2 建立SecurityManager SecurityManager securityManager=factory.getInstance(); //3 將SecurityManager設置當前的運行環境中 SecurityUtils.setSecurityManager(securityManager); //4 從SecurityUtils裏邊建立一個subject Subject subject=SecurityUtils.getSubject(); //5 在認證提交前準備token(令牌) UsernamePasswordToken token =new UsernamePasswordToken("zhangsan", "111111"); try { //6 執行認證提交 subject.login(token); } catch (Exception e) { e.printStackTrace(); } //是否定證經過 boolean isAuthenticated=subject.isAuthenticated(); System.out.println("是否定證經過:"+isAuthenticated); subject.logout(); //是否定證經過 boolean isAuthenticated2=subject.isAuthenticated(); System.out.println("是否定證經過:"+isAuthenticated2); }