第一章 权限概述
第二章 shiro概述
第三章 shiro入门
建模块
改pom
<dependencies>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.3version>
dependency>
<dependency>
<groupId>org.apache.shirogroupId>
<artifactId>shiro-coreartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
dependency>
dependencies>
编写shiro.ini
编写HelloShiro
public class HelloShiro {
@Test
public void shirologin() {
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jay", "123");
subject.login(usernamePasswordToken);
System.out.println("登陆结果"+subject.isAuthenticated());
}
}
所以一般继承授权的类就行了
自定义一个realm
public class DefinitionRealm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String loginName = (String) token.getPrincipal();
System.out.println(loginName);
SecurityService securityService = new SecurityServiceImpl();
String password = securityService.findPasswordByLoginName(loginName);
if("".equals(password)) {
throw new UnknownAccountException("账号不存在");
}
return new SimpleAuthenticationInfo(loginName, password, getName());
}
}
public class SecurityServiceImpl implements SecurityService {
@Override
public String findPasswordByLoginName(String loginName) {
return "123";
}
}
public interface SecurityService {
public String findPasswordByLoginName(String loginName);
}
public class HelloShiro {
@Test
public void shirologin() {
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jay", "123");
subject.login(usernamePasswordToken);
System.out.println("登陆结果"+subject.isAuthenticated());
}
}
shiro.ini
[main]
definitionRealm=com.chenjian.realm.DefinitionRealm
securityManager.realms=$definitionRealm
public class EncodesUtil {
public static String encodeHex(byte[] input) {
return Hex.encodeToString(input);
}
public static byte[] decodeHex(String input) {
return Hex.decode(input);
}
public static String encodeBase64(byte[] input) {
return Base64.encodeToString(input);
}
public static byte[] decodeBase64(String input) {
return Base64.decode(input);
}
}
public class ClientTest {
@Test
public void testHex() {
String val = "hello";
String flag = EncodesUtil.encodeHex(val.getBytes());
System.out.println(flag);
String valHandler = new String(EncodesUtil.decodeHex(flag));
System.out.println(valHandler);
System.out.println("比较字符串是否相等"+val.equals(valHandler));
}
}
@Test
public void testBase64() {
String val = "hello";
String flag = EncodesUtil.encodeBase64(val.getBytes());
System.out.println(flag);
String valHandler = new String(EncodesUtil.decodeBase64(flag));
System.out.println(valHandler);
System.out.println("比较字符串是否相等"+val.equals(valHandler));
}
public class DigestsUtil {
private static final String SHAL="SHA-1";
private static final Integer ITERATIONS = 512;
public static String shal(String input, String salt) {
return new SimpleHash(SHAL, input, salt, ITERATIONS).toString();
}
public static String generateSalt() {
SecureRandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
return randomNumberGenerator.nextBytes().toHex();
}
public static Map<String, String> entryptPassword(String passwordPlan) {
Map<String, String> map = new HashMap<>();
String salt = generateSalt();
String password = shal(passwordPlan, salt);
map.put("salt", salt);
map.put("password", password);
return map;
}
}
public class ClientTest {
@Test
public void testDigestsUtil() {
Map<String, String> map = DigestsUtil.entryptPassword("123");
System.out.println(map.toString());
}
}
改造service
DefinitionRealm
public class DefinitionRealm extends AuthorizingRealm {
public DefinitionRealm() {
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(DigestsUtil.SHAL);
hashedCredentialsMatcher.setHashIterations(DigestsUtil.ITERATIONS);
setCredentialsMatcher(hashedCredentialsMatcher);
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String loginName = (String) token.getPrincipal();
System.out.println(loginName);
SecurityService securityService = new SecurityServiceImpl();
Map<String, String> map = securityService.findPasswordByLoginName(loginName);
if(map.isEmpty()) {
throw new UnknownAccountException("账号不存在");
}
String salt = map.get("salt");
String password = map.get("password");
return new SimpleAuthenticationInfo(loginName, password, ByteSource.Util.bytes(salt), getName());
}
}
SecurityServiceImpl
public class SecurityServiceImpl implements SecurityService {
@Override
public Map<String, String> findPasswordByLoginName(String loginName) {
return DigestsUtil.entryptPassword("123");
}
}
DigestsUtil
public class DigestsUtil {
public static final String SHAL="SHA-1";
public static final Integer ITERATIONS = 512;
public static String shal(String input, String salt) {
return new SimpleHash(SHAL, input, salt, ITERATIONS).toString();
}
public static String generateSalt() {
SecureRandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
return randomNumberGenerator.nextBytes().toHex();
}
public static Map<String, String> entryptPassword(String passwordPlan) {
Map<String, String> map = new HashMap<>();
String salt = generateSalt();
String password = shal(passwordPlan, salt);
map.put("salt", salt);
map.put("password", password);
return map;
}
}
HelloShiro
public class HelloShiro {
@Test
public void shirologin() {
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jay", "123");
subject.login(usernamePasswordToken);
System.out.println("登陆结果"+subject.isAuthenticated());
}
}
shiro.ini
[main]
definitionRealm=com.chenjian.realm.DefinitionRealm
securityManager.realms=$definitionRealm
DefinitionRealm
public class DefinitionRealm extends AuthorizingRealm {
public DefinitionRealm() {
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(DigestsUtil.SHAL);
hashedCredentialsMatcher.setHashIterations(DigestsUtil.ITERATIONS);
setCredentialsMatcher(hashedCredentialsMatcher);
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String loginName = (String) principals.getPrimaryPrincipal();
SecurityService securityService = new SecurityServiceImpl();
List<String> roles = securityService.findRoleByLoginName(loginName);
List<String> permissions = securityService.findPermissionByLoginName(loginName);
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.addRoles(roles);
simpleAuthorizationInfo.addStringPermissions(permissions);
return simpleAuthorizationInfo;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String loginName = (String) token.getPrincipal();
System.out.println(loginName);
SecurityService securityService = new SecurityServiceImpl();
Map<String, String> map = securityService.findPasswordByLoginName(loginName);
if(map.isEmpty()) {
throw new UnknownAccountException("账号不存在");
}
String salt = map.get("salt");
String password = map.get("password");
return new SimpleAuthenticationInfo(loginName, password, ByteSource.Util.bytes(salt), getName());
}
}
SecurityServiceImpl
public class SecurityServiceImpl implements SecurityService {
@Override
public Map<String, String> findPasswordByLoginName(String loginName) {
return DigestsUtil.entryptPassword("123");
}
@Override
public List<String> findRoleByLoginName(String loginName) {
List<String> list = new ArrayList<>();
list.add("admin");
list.add("dev");
return list;
}
@Override
public List<String> findPermissionByLoginName(String loginName) {
List<String> list = new ArrayList<>();
list.add("order:add");
list.add("order:list");
list.add("order:del");
return list;
}
}
HelloShiro
public class HelloShiro {
@Test
public void testPermissionrealm() {
Subject subject = shirologin();
System.out.println("登陆结果"+subject.isAuthenticated());
System.out.println("是否有管理员角色"+subject.hasRole("admin"));
try {
subject.checkRole("coder");
System.out.println("当前用户有coder角色");
}catch (Exception e){
System.out.println("当前用户没有coder角色");
}
System.out.println("是否有查看订单的权限"+subject.isPermitted("order:list"));
try {
subject.checkPermission("order:update");
System.out.println("当前用户有修改的权限");
}catch (Exception e){
System.out.println("当前用户没有修改的权限");
}
}
public Subject shirologin() {
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jay", "123");
subject.login(usernamePasswordToken);
System.out.println("登陆结果"+subject.isAuthenticated());
return subject;
}
}
DigestsUtil
public class DigestsUtil {
public static final String SHAL="SHA-1";
public static final Integer ITERATIONS = 512;
public static String shal(String input, String salt) {
return new SimpleHash(SHAL, input, salt, ITERATIONS).toString();
}
public static String generateSalt() {
SecureRandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
return randomNumberGenerator.nextBytes().toHex();
}
public static Map<String, String> entryptPassword(String passwordPlan) {
Map<String, String> map = new HashMap<>();
String salt = generateSalt();
String password = shal(passwordPlan, salt);
map.put("salt", salt);
map.put("password", password);
return map;
}
}
shiro.ini
#声明自定义的realm,且为安全管理器指定realms
[main]
definitionRealm=com.chenjian.realm.DefinitionRealm
securityManager.realms=$definitionRealm
第四章 Web项目集成Shiro