2021SC@SDUSC
JdbcRealm的类继承关系图
JdbcRealm测试
package com.shiro.test; import com.alibaba.druid.pool.DruidDataSource; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.DefaultSecurityManager; import org.apache.shiro.realm.jdbc.JdbcRealm; import org.apache.shiro.subject.Subject; import org.junit.Test; public class JdbcRealmTest { DruidDataSource dataSource = new DruidDataSource(); { dataSource.setUrl("jdbc:mysql://localhost:3306/shiro"); dataSource.setUsername("root"); dataSource.setPassword("123456"); } @Test public void testAuthentication(){ JdbcRealm jdbcRealm = new JdbcRealm(); jdbcRealm.setDataSource(dataSource); jdbcRealm.setPermissionsLookupEnabled(true); String sql = "select password from test_user where user_name = ?"; jdbcRealm.setAuthenticationQuery(sql); //1.构建securityManager环境 DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager(); defaultSecurityManager.setRealm(jdbcRealm); //2.主体提交认证请求 SecurityUtils.setSecurityManager(defaultSecurityManager); Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("Alax","654321"); subject.login(token); System.out.println(token.getUsername()+" isAuthenticated:"+subject.isAuthenticated()); subject.checkRole("admin"); subject.checkPermission("user:select"); subject.logout(); System.out.println(token.getUsername()+" isAuthenticated:"+subject.isAuthenticated()); } }
测试结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)