目录
shiro认证,盐加密
今天整合ssm和shiro,完成登录、注册的密码加密
首先一样需要导入pom依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency>
然后需要配置web.xm
<!-- shiro过滤器定义 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后我们逆向生成的ShiroUser就不发了,然后我们需要在写一个根据用户名查单个的方法,因为我们需要根据用户名查询出用户所有信息,然后再把密码和盐进行判断
这是我的ShiroUserService
package com.xy.service;
import com.xy.model.ShiroUser;
/**
* @author 阳某
* @create 2019-10-13 16:17
*/
public interface ShiroUserService {
/**
* 用来shiro认证的
* @param uname
* @return
*/
ShiroUser queryByName(String uname);
int insert(ShiroUser shiroUser);
}
MyRealm,调用queryByName获取用户信息,然后对用户名和密码进行判断
package com.xy.shiro;
import com.xy.model.ShiroUser;
import com.xy.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;
/**
* @author 阳某
* @create 2019-10-13 16:13
*
*/
public class MyRealm extends AuthorizingRealm {
private ShiroUserService shiroUserService;
public ShiroUserService getShiroUserService() {
return shiroUserService;
}
public void setShiroUserService(ShiroUserService shiroUserService) {
this.shiroUserService = shiroUserService;
}
/**
* 授权
* @param principals
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null;
}
/**
* 认证
*
* 认证的过程
* 1、数据源(ini)现在数据库
* 2、doGetAuthenticationInfo将数据库的用户信息给subject主体做shiro认证
* 2.1、需要在当前realm中调用service来验证,当前用户是覅有在数据库中存在
* 2.2、盐加密
*
* @param token 从jsp传递过来的用户名密码组成的一个token对象
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String userName = token.getPrincipal().toString();
String pwd = token.getCredentials().toString();
ShiroUser shiroUser = this.shiroUserService.queryByName(userName);
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
shiroUser.getUsername(),
shiroUser.getPassword(),
ByteSource.Util.bytes(shiroUser.getSalt()),
this.getName()
);
return info;
}
}
ShiroUserController
package com.xy.controller;
import com.xy.model.ShiroUser;
import com.xy.service.ShiroUserService;
import com.xy.util.PasswordHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 阳某
* @create 2019-10-13 16:36
*/
@Controller
public class ShiroUserController {
@Autowired
private ShiroUserService shiroUserService;
@RequestMapping("/login")
public String login(HttpServletRequest req, HttpServletResponse resp){
Subject subject = SecurityUtils.getSubject();
String uname = req.getParameter("username");
String pwd = req.getParameter("password");
UsernamePasswordToken token = new UsernamePasswordToken(uname, pwd);
try {
subject.login(token);
req.setAttribute("uname",uname);
return "main";
}catch (Exception e){
req.setAttribute("message","用户名或密码错误");
return "login";
}
}
@RequestMapping("/logout")
public String logout(HttpServletRequest req, HttpServletResponse resp){
Subject subject = SecurityUtils.getSubject();
subject.logout();
return "login";
}
/**
* 把加密后的密码存入数据库中
* @param req
* @param resp
* @return
*/
@RequestMapping("/register")
public String register(HttpServletRequest req, HttpServletResponse resp){
String uname = req.getParameter("username");
String pwd = req.getParameter("password");
String salt = PasswordHelper.createSalt();
String credentials = PasswordHelper.createCredentials(pwd, salt);
ShiroUser shiroUser=new ShiroUser();
shiroUser.setUsername(uname);
shiroUser.setPassword(credentials);
shiroUser.setSalt(salt);
int insert = shiroUserService.insert(shiroUser);
if(insert>0){
req.setAttribute("message","注册成功");
return "login";
}
else{
req.setAttribute("message","注册失败");
return "login";
}
}
}
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<script type="text/javascript">
function register(){
}
</script>
<body>
<h1>用户登陆</h1>
<div style="color: red">${message}</div>
<form action="${pageContext.request.contextPath}/login" method="post">
帐号:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="确定">
<input type="button" onclick="location.href='${pageContext.request.contextPath}/register.jsp'" value="注册">
<input type="reset" value="重置">
</form>
</body>
</html>
register.jsp
<%--
Created by IntelliJ IDEA.
User: 86135
Date: 2019/10/13
Time: 16:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户注册</title>
</head>
<body>
<h1>用户注册</h1>
<form action="${pageContext.request.contextPath}/register" method="post">
帐号:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="注册">
<input type="button" onclick="location.href='${pageContext.request.contextPath}/login.jsp'" value="返回">
</form>
</body>
</html>
然后存入数据库中的便不在是密码’123’,而是我们加密后的随机字符串
end…