目录
1、导入jar包,thymeleaf和shiro-spring
3、创建config文件夹,编写配置类ShiroConfig类
3.1、首先创建realm对象,在config文件夹里创建UserRealm类
3.3、配置DefaultWebSecurityManager,并且关联UserRealm
4.1、templates文件夹下创建一个user文件夹,写两个页面add.html和update.html
4.3、在index.html页面中,写两个a链接,href地址就是映射地址
5.2.2、在MyController里面写一个去到登录页的跳转
在之前的springboot-07-shiro项目里创建一个springboot项目shiro-springboot,选择添加spring-web依赖

在templates文件夹下写一个index.html页面

然后再创建controller文件夹,编写MyController类跳转页面


ShiroConfig类里面需要有3个核心要素:ShiroFilterFactoryBean;DefaultWebSecurityManager;创建realm对象,需要自定义一个类。
3个核心要素的配置顺序倒着配置
这个类里面基本就是Security里面的写授权和认证的地方

3.2、把UserRealm这个对象 bean 放到Configuration里面


并关联DefaultWebSecurityManager,设置安全管理器


就是在这个类里面加两个方法







步骤:写login.html ==> 在controller里面编写toLogin方法 ==> 在ShiroConfig里面设置登录请求




- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.4version>
- <relativePath/>
- parent>
- <groupId>com.zhougroupId>
- <artifactId>shiro-springbootartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>shiro-springbootname>
- <description>shiro-springbootdescription>
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
-
-
- <dependency>
- <groupId>org.apache.shirogroupId>
- <artifactId>shiro-springartifactId>
- <version>1.4.1version>
- dependency>
-
- <dependency>
- <groupId>org.thymeleafgroupId>
- <artifactId>thymeleaf-spring5artifactId>
- dependency>
- <dependency>
- <groupId>org.thymeleaf.extrasgroupId>
- <artifactId>thymeleaf-extras-java8timeartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
-
- project>
- package com.zhou.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";
- }
-
-
- }
- package com.zhou.config;
-
- import org.apache.shiro.authc.AuthenticationException;
- import org.apache.shiro.authc.AuthenticationInfo;
- import org.apache.shiro.authc.AuthenticationToken;
- import org.apache.shiro.authz.AuthorizationInfo;
- import org.apache.shiro.realm.AuthorizingRealm;
- import org.apache.shiro.subject.PrincipalCollection;
-
- // 自定义的UserRealm extends AuthorizingRealm
- public class UserRealm extends AuthorizingRealm {
- // 授权
- @Override
- protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
- System.out.println("执行了====>授权doGetAuthorizationInfo");
- return null;
- }
-
- // 认证
- @Override
- protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
- System.out.println("执行了====>认证doGetAuthorizationInfo");
- return null;
- }
- }
- package com.zhou.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.beans.factory.annotation.Required;
- 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 getDefaultWebSecurityManager){
- ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
- // 关联 DefaultWebSecurityManager 设置安全管理器
- bean.setSecurityManager(getDefaultWebSecurityManager);
-
- // 添加shiro的内置过滤器
- /*
- * anon:无需认证就可以访问
- * authc:必须认证了才能访问
- * user:必须拥有 记住我 功能才能用
- * perms:拥有对某个资源的权限才能访问
- * role:拥有某个权限才能访问
- *
- * */
- Map
filterMap = new LinkedHashMap<>(); -
- filterMap.put("/user/add", "authc"); // 表示被认证的用户才能访问这个页面,之前没有配置这行代码就是可以直接点进add页面
- filterMap.put("/user/update", "authc");
-
- bean.setFilterChainDefinitionMap(filterMap);
-
- // 设置登录的请求
- bean.setLoginUrl("/toLogin");
-
- return bean;
- }
-
- // DefaultWebSecurityManager 第二步
- @Bean(name = "securityManager")
- public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
- DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
- // 关联UserRealm
- securityManager.setRealm(userRealm);
- return securityManager;
- }
-
- // 创建realm对象,需要自定义一个类 第一步
- @Bean
- public UserRealm userRealm(){
- return new UserRealm();
- }
- }