Open NewBrandSTONE opened 5 years ago
新建 shiro-demo 工程
创建 shiro-test module
在 shiro-test 中引入相关依赖
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>shiro-demo</artifactId> <groupId>com.ozyoung.shiro</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>shiro-test</artifactId> <dependencies> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> </dependencies> </project>
No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration.
package com.young.test; import org.apache.shiro.SecurityUtils; import org.apache.shiro.mgt.DefaultSecurityManager; import org.apache.shiro.realm.SimpleAccountRealm; import org.apache.shiro.subject.Subject; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class AuthenticationTest { private Logger logger = LoggerFactory.getLogger(getClass()); private SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm(); @Before public void addUser() { simpleAccountRealm.addAccount("mike", "123456"); } @Test public void testAuthentication() { // 1.构建SecurityManager环境 DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager(); defaultSecurityManager.setRealm(simpleAccountRealm); // 2.主体提交认证 Subject subject = SecurityUtils.getSubject(); boolean isAuthenticated = subject.isAuthenticated(); logger.info("主体认证-{}", isAuthenticated); } }
package com.young.test; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.mgt.DefaultSecurityManager; import org.apache.shiro.realm.SimpleAccountRealm; import org.apache.shiro.subject.Subject; import org.junit.Before; import org.junit.Test; public class AuthenticationTest { private SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm(); @Before public void addUser() { simpleAccountRealm.addAccount("mike", "123456"); } @Test public void testAuthentication() { // 1.构建SecurityManager环境 DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager(); defaultSecurityManager.setRealm(simpleAccountRealm); // 2.设置SecurityManager SecurityUtils.setSecurityManager(defaultSecurityManager); // 2.主体提交认证 Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("mike", "123456"); subject.login(usernamePasswordToken); boolean isAuthenticated = subject.isAuthenticated(); System.out.println("主体认证-" + isAuthenticated); } }
使用 Subject.login(UsernamePasswordToken)
在 Subject.login(token) 的内部是通过调用 SecurityManager.login(delegateSubject, token)
DefaultSercurityManager.login 方法调用 AuthenticatingSecurityManager.authenticate(token) 方法来实现
authenticate 方法中,使用 authenticator.authenticate(token) 方法实现认证。
authenticator 中调用 ModularRealmAuthenticator.doAuthenticate 方法实现认证
Shiro 的认证过程
新建 shiro-demo 工程
创建 shiro-test module
在 shiro-test 中引入相关依赖
核心思想
踩下的坑
No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration.
认证过程分析
使用 Subject.login(UsernamePasswordToken)
在 Subject.login(token) 的内部是通过调用 SecurityManager.login(delegateSubject, token)
DefaultSercurityManager.login 方法调用 AuthenticatingSecurityManager.authenticate(token) 方法来实现
authenticate 方法中,使用 authenticator.authenticate(token) 方法实现认证。
authenticator 中调用 ModularRealmAuthenticator.doAuthenticate 方法实现认证