一、盐加密
继续在之前的ssm项目上续写
(一)Shiro环境配置与表生成
1、导入pom相关依赖
org.apache.shiro
shiro-core
1.3.2
org.apache.shiro
shiro-web
1.3.2
org.apache.shiro
shiro-spring
1.3.2
2、web.xml配置
shiroFilter
org.springframework.web.filter.DelegatingFilterProxy
targetFilterLifecycle
true
shiroFilter
private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
private static final String hashAlgorithmName = "md5";
private static final int hashIterations = 1024;
private static final boolean storedCredentialsHexEncoded = true;
public static String createSalt() {
return randomNumberGenerator.nextBytes().toHex();
}
public static String createCredentials(String credentials, String salt) {
SimpleHash simpleHash = new SimpleHash(hashAlgorithmName, credentials,
salt, hashIterations);
return storedCredentialsHexEncoded ? simpleHash.toHex() : simpleHash.tobase64();
}
public static boolean checkCredentials(String credentials, String salt, String encryptCredentials) {
return encryptCredentials.equals(createCredentials(credentials, salt));
}
public static void main(String[] args) {
//盐
String salt = createSalt();
System.out.println(salt);
System.out.println(salt.length());
//凭证+盐加密后得到的密码
String credentials = createCredentials("123", salt);
System.out.println(credentials);
System.out.println(credentials.length());
boolean b = checkCredentials("123", salt, credentials);
System.out.println(b);
}
}
2、运行获得结果
1、导入pom相关依赖
org.apache.shiro shiro-core1.3.2 org.apache.shiro shiro-web1.3.2 org.apache.shiro shiro-spring1.3.2
2、web.xml配置
shiroFilter
org.springframework.web.filter.DelegatingFilterProxy
targetFilterLifecycle
true
shiroFilter
private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
private static final String hashAlgorithmName = "md5";
private static final int hashIterations = 1024;
private static final boolean storedCredentialsHexEncoded = true;
public static String createSalt() {
return randomNumberGenerator.nextBytes().toHex();
}
public static String createCredentials(String credentials, String salt) {
SimpleHash simpleHash = new SimpleHash(hashAlgorithmName, credentials,
salt, hashIterations);
return storedCredentialsHexEncoded ? simpleHash.toHex() : simpleHash.tobase64();
}
public static boolean checkCredentials(String credentials, String salt, String encryptCredentials) {
return encryptCredentials.equals(createCredentials(credentials, salt));
}
public static void main(String[] args) {
//盐
String salt = createSalt();
System.out.println(salt);
System.out.println(salt.length());
//凭证+盐加密后得到的密码
String credentials = createCredentials("123", salt);
System.out.println(credentials);
System.out.println(credentials.length());
boolean b = checkCredentials("123", salt, credentials);
System.out.println(b);
}
}
2、运行获得结果
第一次运行:
第二次运行:
两次运行的结果都不相同,但同时都是123;安全系数高
二、Shiro认证
1、Myrealm.java
自定义Realm
Shiro从Realm获取安全数据(如用户、角色、权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法;也需要从Realm得到用户相应的角色/权限进行验证用户是否能进行操作;可以把Realm看成DataSource,即安全数据源。
最基础的是Realm接口,CachingRealm负责缓存处理,AuthenticationRealm负责认证,AuthorizingRealm负责授权。
通常自定义的realm继承AuthorizingRealm
注1:体系结构见“shiro提供的realm.png”
AuthorizationInfo:授权信息
AuthenticationInfo:认证信息
自定义Realm
Shiro从Realm获取安全数据(如用户、角色、权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法;也需要从Realm得到用户相应的角色/权限进行验证用户是否能进行操作;可以把Realm看成DataSource,即安全数据源。最基础的是Realm接口,CachingRealm负责缓存处理,AuthenticationRealm负责认证,AuthorizingRealm负责授权。
通常自定义的realm继承AuthorizingRealm
注1:体系结构见“shiro提供的realm.png”
AuthorizationInfo:授权信息
AuthenticationInfo:认证信息
继承AuthorizingRealm需要重写两个方法;重写授权与认证
package com.lsy.shiro;
import com.lsy.model.ShiroUser;
import com.lsy.service.ShiroUserService;
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.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;
public class MyRealm extends AuthorizingRealm {
private ShiroUserService shiroUserService;
public ShiroUserService getShiroUserService() {
return shiroUserService;
}
public void setShiroUserService(ShiroUserService shiroUserService) {
this.shiroUserService = shiroUserService;
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("身份认证...");
//token就是controller层中的login请求,subject.login(token)
String username = token.getPrincipal().toString();
String password = token.getCredentials().toString();
ShiroUser user = shiroUserService.queryByName(username);
// 拿到数据库中的用户信息,放入token凭证中,用于controler进行对比
AuthenticationInfo info = new SimpleAuthenticationInfo(
user.getUsername(),
user.getPassword(),
ByteSource.Util.bytes(user.getSalt()),
this.getName()
);
return info;
}
}
2、applicationContext-shiro.xml
MyRealm要交给spring进行管理需要在applicationContext-shiro.xml中进行配置
/user/login=anon /user/updatePwd.jsp=authc /admin @Controller public class ShiroUserController { @RequestMapping("/login") public String login(HttpServletRequest req, HttpServletResponse resp){ String username = req.getParameter("username"); String password = req.getParameter("password"); UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password); Subject subject = SecurityUtils.getSubject(); try { subject.login(usernamePasswordToken); req.getRequestDispatcher("main.jsp").forward(req, resp); } catch (Exception e) { req.setAttribute("message", "您的用户名密码输入有误!!!"); try { req.getRequestDispatcher("login.jsp").forward(req, resp); } catch (ServletException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } return null; } @RequestMapping("/logout") public String logout(HttpServletRequest req, HttpServletResponse resp){ Subject subject = SecurityUtils.getSubject(); subject.logout(); try { resp.sendRedirect(req.getContextPath()+"/login.jsp"); } catch (IOException e) { e.printStackTrace(); } return null; } }
4、导入相关jsp界面
5、 将applicationContext-shiro.xml交给spring进行管理
6、结果
这与shiro入门测试结果一致,变化的是shiro入门用的是死数据(ini文件),
而这次用的是数据库数据
①、不填写登录
②、不同角色登录,查看到的功能各不相同
张三:
③、权限查看
王五登录点击用户新增显示界面



