为啥子要自定义资源认证规则?什么是自定义资源认证规则
答:因为我们知道SpringBoot整合SpringSecurity项目,只需要一个依赖,便可以将所有的请求进行了默认处理,就是所有的请求必须要认证,但是我们会有需求说,有些公共资源无需要认证如首页轮播图等
,有些资源需要认证,所以就有了自定义资源认证规则的设置。
第一步:创建springboot项目
<?xml version="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.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-security-02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-security-02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二步:添加配置application.properties
#修改springSecurity默认用户名和密码
spring.security.user.name=root
spring.security.user.password=root
第三步:Controller
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
System.out.println("hello spring security");
return "hello spring security";
}
}
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@GetMapping("/index")
public String hello(){
System.out.println("hello index");
return "hello index";
}
}
第四步:添加自定义自定义资源认证规则
package com.example.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
/**
* 自定义资源认证规则
* 背景:我们此时此刻有/index和/hello两个资源
* 需求:我们希望/index资源无需登录,直接访问
* 希望除/index外之前资源需要表单认证
* @param http
* @throws Exception
*/
@Override
public void configure(HttpSecurity http) throws Exception {
//【注意事项】放行资源要放在前面,认证的放在后面
http.authorizeRequests()
.mvcMatchers("/index").permitAll() //代表放行index的所有请求
.anyRequest().authenticated()//代表其他请求需要认证
.and()
.formLogin();//表示其他需要认证的请求通过表单认证
}
}
说明
- permitAll() 代表放行该资源,该资源为公共资源 无需认证和授权可以直接访问
- anyRequest().authenticated() 代表所有请求,必须认证之后才能访问
- formLogin() 代表开启表单认证
- 注意:放行资源必须放在所有认证请求之前
第五步:测试
登录后就可以访问了