2024.5.22 Wednesday
接上文【WEEK13】 【DAY2】Shiro第二部分【中文版】
目录
- 15.4.登录拦截
- 15.4.1.修改ShiroConfig.java
- 15.4.2.新建login.html页面
- 15.4.3.修改MyController.java
- 15.4.4.再次修改ShiroConfig.java
- 15.4.5.重启
- 15.5.用户认证
- 15.5.1.修改MyController.java
- 15.5.2.修改login.html
- 15.5.3.重启
- 15.5.4.修改UserRealm.java
- 15.5.5.重启
- 15.5.5.1.输入正确用户名和密码
- 15.5.5.2.输入错误用户名
- 15.5.5.3.输入错误密码
15.4.登录拦截
15.4.1.修改ShiroConfig.java
package com.P40.config;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
//ShiroFilterFactoryBean
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
//要配置什么:点击进入ShiroFilterFactoryBean源码查看
//设置安全管理器
bean.setSecurityManager(defaultWebSecurityManager);
//添加shiro的内置过滤器
Map<String,String> filterMap = new LinkedHashMap<>();
filterMap.put("/user/add","authc");
filterMap.put("/user/update","authc");
//仅在此处修改add和update页面的访问权限后,重启项目,点击add或update都将被拦截,显示404错误,希望跳转到登录页面
bean.setFilterChainDefinitionMap(filterMap);
return bean;
}
//DefaultWebSecurityManager
...
}
- anon: 无需认证就可以访问
- authc: 必须认证了才能访问
- user: 必须拥有记住我功能才能用
- perms: 拥有对某个资源的权限才能访问
- role: 拥有某个角色权限
15.4.2.新建login.html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>登录</h1>
<hr>
<form action="">
<p>用户名:<input type="text" name="username"></p>
<p>密码:<input type="text" name="password"></p>
<p><input type="submit"></p>
</form>
</body>
</html>
15.4.3.修改MyController.java
添加访问login页面的方法
package com.P40.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping({"/", "/index"})
public String toIndex(Model model){
model.addAttribute("msg","hello, Shiro");
return "index";
}
@RequestMapping("/user/add")
public String add(){
return "user/add";
}
@RequestMapping("/user/update")
public String update(){
return "user/update";
}
@RequestMapping("/toLogin")
public String toLogin(){
return "login";
}
}
15.4.4.再次修改ShiroConfig.java
使用通配符,跳转登录页
package com.P40.config;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
//ShiroFilterFactoryBean
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
//要配置什么:点击进入ShiroFilterFactoryBean源码查看
//设置安全管理器
bean.setSecurityManager(defaultWebSecurityManager);
//添加shiro的内置过滤器
/*
anon: 无需认证就可以访问
authc: 必须认证了才能访问
user: 必须拥有记住我功能才能用
perms: 拥有对某个资源的权限才能访问
role: 拥有某个角色权限
*/
//登录拦截
Map<String,String> filterMap = new LinkedHashMap<>();
// filterMap.put("/user/add","authc");
// filterMap.put("/user/update","authc");
//仅在此处修改add和update页面的访问权限后,重启项目,点击add或update都将被拦截,显示404错误,希望跳转到登录页面
filterMap.put("/user/*","authc"); //也可以使用通配符实现(替代上两行的/user/add和/user/update)
bean.setFilterChainDefinitionMap(filterMap);
//如果没有权限,则需要跳转到登录页
bean.setLoginUrl("/toLogin"); //设置登录的请求
return bean;
}
//DefaultWebSecurityManager
@Bean(name = "securityManager") //给这个类起别名,便于ShiroFilterFactoryBean调用
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){ //获取UserRealm,但是这里好像不需要使用注解,可以直接调用
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//默认的DefaultWebSecurityManager类的类名是defaultWebSecurityManager,只是在这里修改成securityManager
//关联UserRealm
securityManager.setRealm(userRealm);
return securityManager;
}
//创建realm对象,需要自定义类
@Bean
public UserRealm userRealm(){
return new UserRealm();
}
//创建时顺序是相反的(从real->DefaultWebSecurityManager->ShiroFilterFactoryBean)
}
15.4.5.重启
在首页点击add或者update后跳转到登录页
http://localhost:8080/toLogin
15.5.用户认证
15.5.1.修改MyController.java
package com.P40.controller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping({"/", "/index"})
public String toIndex(Model model){
model.addAttribute("msg","hello, Shiro");
return "index";
}
@RequestMapping("/user/add")
public String add(){
return "user/add";
}
@RequestMapping("/user/update")
public String update(){
return "user/update";
}
@RequestMapping("/toLogin")
public String toLogin(){
return "login";
}
@RequestMapping("/login")
public String login(String username, String password, Model model){
//获取当前的用户
Subject subject = SecurityUtils.getSubject();
//封装用户的登录数据
UsernamePasswordToken token = new UsernamePasswordToken(username,password);
//执行登录的方法,没有异常则成功了,选中代码后Ctrl+Alt+T添加try catch
try {
subject.login(token);
return "index";
} catch (UnknownAccountException e) { //用户名不存在
model.addAttribute("msg","用户名错误");
return "login";
} catch (IncorrectCredentialsException e) { //密码错误
model.addAttribute("msg","密码错误");
return "login";
}
}
}
15.5.2.修改login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>登录</h1>
<hr>
<p th:text="${msg}" style="color: cadetblue"></p>
<form th:action="@{/login}">
<p>用户名:<input type="text" name="username"></p>
<p>密码:<input type="text" name="password"></p>
<p><input type="submit"></p>
</form>
</body>
</html>
15.5.3.重启
任意输入一组用户名和密码
说明执行了
15.5.4.修改UserRealm.java
package com.P40.config;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
//UserRealm是一个bean
//自定义的UserRealm,必须要继承AuthorizingRealm方法,然后implement methods(alt+insert)
public class UserRealm extends AuthorizingRealm {
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("do doGetAuthorizationInfo 授权");
return null;
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("do doGetAuthorizationInfo 认证");
//用户名,密码————数据读取
String name = "root";
String password = "1";
UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;
if (!userToken.getUsername().equals(name)){
return null; //抛出UnknownAccountException异常(自动的)
}
//密码认证,shiro运行
return new SimpleAuthenticationInfo("",password,"");
}
}
15.5.5.重启
任选add或update点击,自动跳转到登录页面。
15.5.5.1.输入正确用户名和密码
提交后返回到首页,此时add页面和update页面均可访问
15.5.5.2.输入错误用户名
15.5.5.3.输入错误密码