shiro实现认证+授权简单实现(不连数据库)
- 自定义realm继承AuthorizingRealm
- 实现其中两个方法doGetAuthorizationInfo(授权),doGetAuthenticationInfo(认证),在方法中编写业务成需求
- 返回值是SimpleAuthenticationInfo simpleAuthorizationInfo Simple**(授权,认证)
package com.jing.md5;
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.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import java.util.Arrays;
public class CustomMd5Realm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String primaryPrincipal = (String) principals.getPrimaryPrincipal();
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.addRole("user");
simpleAuthorizationInfo.addRole("admin");
simpleAuthorizationInfo.addStringPermissions(Arrays.asList("user:*","product:create:01"));
return simpleAuthorizationInfo;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String principal = (String) token.getPrincipal();
if ("xiaojing".equals(principal)) {
return new SimpleAuthenticationInfo(principal, "7a3865843b5ac72bb4d9e28cd8877681", ByteSource.Util.bytes("zard"), this.getName());
}
return null;
}
}
package com.jing.md5;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import java.util.Arrays;
public class TestCustomMd5Realm {
public static void main(String[] args) {
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
CustomMd5Realm realm = new CustomMd5Realm();
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
hashedCredentialsMatcher.setHashAlgorithmName("md5");
hashedCredentialsMatcher.setHashIterations(1024);
realm.setCredentialsMatcher(hashedCredentialsMatcher);
defaultSecurityManager.setRealm(realm);
SecurityUtils.setSecurityManager(defaultSecurityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("xiaojing", "123");
try {
subject.login(token);
System.out.println("登录成功");
if (subject.isAuthenticated()) {
boolean user = subject.hasRole("user");
boolean admin = subject.hasRole("admin");
System.out.println("admin = " + admin);
System.out.println("user = " + user);
boolean allRoles = subject.hasAllRoles(Arrays.asList("user", "admin","super"));
System.out.println("allRoles = " + allRoles);
System.out.println("=========================");
boolean[] booleans = subject.hasRoles(Arrays.asList("user", "super", "admin"));
for (boolean aBoolean : booleans) {
System.out.println(aBoolean);
}
boolean permitted = subject.isPermitted("user:create:*");
System.out.println("permitted = " + permitted);
boolean permitted1 = subject.isPermitted("user:create:01");
System.out.println("permitted1 = " + permitted1);
boolean permitted2 = subject.isPermitted("product:create:02");
System.out.println("permitted2 = " + permitted2);
boolean permittedAll = subject.isPermittedAll("user:create", "product:create:02");
System.out.println("permittedAll = " + permittedAll);
boolean[] permitteds = subject.isPermitted("user:delete:01", "product:create:01", "product:create:02");
for (boolean b : permitteds) {
System.out.println(b);
}
}
} catch (UnknownAccountException e) {
e.printStackTrace();
System.out.println("账号错误");
} catch (IncorrectCredentialsException e) {
e.printStackTrace();
System.out.println("密码错误");
} catch (Exception e) {
e.printStackTrace();
}
}
}