自定义的realm
public class MyRealm2 extends AuthorizingRealm {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//权限验证调用
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("权限验证");
String sql = "select ROLE_NAME from SHIRO_USER_ROLE where USER_NAME = ?";
String username = (String) principalCollection.getPrimaryPrincipal();
List<String> roles = jdbcTemplate.queryForList(sql, String.class, username);
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addRoles(roles);
return info;
}
//登录的时候调用
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("doGetAuthenticationInfo");
String sql = "select PASSWORD from SHIRO_USER where USER_NAME = ?";
String username = (String) authenticationToken.getPrincipal();
String password = jdbcTemplate.queryForObject(sql, String.class, username);
SimpleAuthenticationInfo info =
new SimpleAuthenticationInfo(username, password, null, getName());
return info;
}
}
shiro-mysql.ini
[main]
dataSource=org.springframework.jdbc.datasource.DriverManagerDataSource
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://127.0.0.1:3306/shiro_test
dataSource.username=root
#如果数据库没有密码,就不要写这行
#dataSource.password=
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
#是否检查权限
jdbcRealm.permissionsLookupEnabled = true
jdbcRealm.dataSource=$dataSource
#重写sql语句
#根据用户名查询出密码
jdbcRealm.authenticationQuery = select PASSWORD from SHIRO_USER where USER_NAME = ?
#根据用户名查询出角色
jdbcRealm.userRolesQuery = select ROLE_NAME from SHIRO_USER_ROLE where USER_NAME = ?
#根据角色名查询出权限
jdbcRealm.permissionsQuery = select PERM_NAME from SHIRO_ROLE_PERMISSION WHERE ROLE_NAME = ?
securityManager.realms=$jdbcRealm
测试类
public class ShiroIniTest {
public static void main(String[] args) {
DefaultSecurityManager securityManager = new DefaultSecurityManager();
ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
securityManager.setAuthenticator(authenticator);
ModularRealmAuthorizer authorizer = new ModularRealmAuthorizer();
authorizer.setPermissionResolver(new WildcardPermissionResolver());
securityManager.setAuthorizer(authorizer);
//dataSource=org.springframework.jdbc.datasource.DriverManagerDataSource
DriverManagerDataSource dataSource = new DriverManagerDataSource();
// dataSource.driverClassName = com.mysql.jdbc.Driver
// dataSource.url = jdbc:mysql://127.0.0.1:3306/shiro_test
// dataSource.username = root
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/shiro_test");
dataSource.setUsername("root");
dataSource.setPassword("");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
MyRealm2 myRealm2 = new MyRealm2();
myRealm2.setJdbcTemplate(jdbcTemplate);
securityManager.setRealm(myRealm2);
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("admin@shiro.com", "admin");
try {
subject.login(token);
System.out.println(subject.hasRole("test"));
System.out.println("登录成功");
} catch (AuthenticationException e) {
//e.printStackTrace();
System.out.println("用户名或密码错误,登录失败");
}
}
}