【shiro篇】 二. shiro与spring整合(idea+maven项目)
篇幅较长可以通过右侧目录查看文章目录
shiro登录认证与授权案例
项目准备
1 登录认证
1.1 新建一个UserController
写一个login方法
package com.wpj.web.controller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/login")
public String login(String username, String password){
// 1. 获取当前用户
Subject currentUser = SecurityUtils.getSubject();
// *2. 判断是否登录,没有登陆返回false
if (!currentUser.isAuthenticated()) {
// 3。 封装用户名和密码
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
// 4. 登录
currentUser.login(token);
} catch (AuthenticationException ae) { // 6.3.4 其他的认证异常
System.out.println("认证失败。。。。");
return "login";
}
}
return "ok";
}
}
1.2 新建一个ok页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录成功</h1>
</body>
</html>
1.3 修改login页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录页面</h1>
<form action="/login">
username:<input type="text" name="username"><br />
password:<input type="text" name="password"><br />
<input type="submit" value="登录">
</form>
</body>
</html>
1.4 写一个UserRealm安全数据源实现AuthorizingRealm
暂时不访问数据库
package com.wpj.realm;
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 java.util.HashSet;
public class UserRealm extends AuthorizingRealm {
/**
* 授权
* @param principalCollection
* @return
*/
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
}
/**
* 认证
* @param authenticationToken
* @return
* @throws AuthenticationException
*/
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
// 获取用户名去判断密码
Object username = authenticationToken.getPrincipal();
// 先不连接数据库 先写死密码
String password = "admin";
// 返回封装的数据
SimpleAuthenticationInfo saci = new SimpleAuthenticationInfo(username, password, getName());
return saci;
}
}
1.5 在spring容器中配置安全数据源
<!-- shiro核心组件 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="sessionMode" value="native" />
<property name="realm" ref="userRealm" />
</bean>
<bean id="userRealm" class="com.wpj.realm.UserRealm">
</bean>
1.6 配置匿名过滤
1.7 运行tomcat并登录测试
在UserRealm写了 密码为admin 用户名暂时不限制
登录成功进入ok.jsp,登录失败则返回login.jsp
2 注销
2.1 在ok.jsp中写一个注销的超链接
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录成功</h1>
<a href="/logout">注销</a><br />
</body>
</html>
2.2 在spring容器中配置注销过滤器
2.3 测试
- 登录进入ok页面后会生成一个JSESSION
- 点击注销就可以注销成功。
- 通过logou注销过滤器会将Cookie中的JSESSION设置为无效状态
3 密码加密
3.1 修改UserRealm配置
<bean id="userRealm" class="com.wpj.realm.UserRealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!-- 加密的算法 -->
<property name="hashAlgorithmName" value="MD5"></property>
<!--加密次数 -->
<property name="hashIterations" value="1024"></property>
</bean>
</property>
</bean>
3.2 写一个MD5加密的工具类
package com.wpj.utils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
public class MD5Util {
public static String encryption(Object source) {
// 1.加密算法
String algorithmName = "MD5";
// 3.盐值
Object salt = ByteSource.Util.bytes(source); // 盐值一般是用户名或者userId
// 4.加密次数
int hashIterations = 1024;
SimpleHash simpleHash = new SimpleHash(algorithmName, source, salt, hashIterations);
return simpleHash.toString();
}
}
3.3 修改UserRealm认证方法doGetAuthenticationInfo
/**
* 认证
* @param authenticationToken
* @return
* @throws AuthenticationException
*/
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
// 获取用户名去判断密码
Object username = authenticationToken.getPrincipal();
// 先不连接数据库 先写死密码
String password = MD5Util.encryption("admin");
// 返回封装的数据
// SimpleAuthenticationInfo saci = new SimpleAuthenticationInfo(username, password, getName());
// 返回加密封装后数据
// 设置盐值
ByteSource salt = ByteSource.Util.bytes(username);
SimpleAuthenticationInfo saci = new SimpleAuthenticationInfo(username,password,salt,getName());
return saci;
}
3.4 测试
登录还是可以登录。。。。。