突然遇到WebSecurityConfigurerAdapter依赖爆红,导致该功能不能正常使用。
是应为pom文件错了,还是版本不同了需要换一种方式使用WebSecurityConfigurerAdapter依赖
java: org.springframework.security.config.annotation.method.configuration中的org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity已过时
package com.example.vue02demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import javax.sql.DataSource;
/**
*
* @author: xxm
* 功能描述: SpringSecurity的配置
* @date: 2020/5/28 15:14
*/
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 需要放行的URL
*/
public static final String[] AUTH_WHITELIST = {
"/user/login",
"/user/registered",
"/food/findname",
"/food/find",
"/user/updatestate",
"/food/insert",
"/shouhou/insert",
// other public endpoints of your API may be appended to this array
};
/**
* 配置请求拦截
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and()
//由于使用的是JWT,我们这里不需要csrf
.csrf().disable()
//基于token,所以不需要session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
//可以匿名访问的链接
.antMatchers(AUTH_WHITELIST).permitAll()
//其他所有请求需要身份认证
.anyRequest().authenticated()
.and()
//.addFilter(new JWTLoginFilter(authenticationManager()))
.addFilter(new JWTAuthenticationFilter(authenticationManager()));
}
}
<?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.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>vue02demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>vue02demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.32</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>3.0.4</version>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>6.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-booter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.akamai</groupId>
<artifactId>EdgeAuth-Token-Java</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>com.zdawn</groupId>
<artifactId>jwt-token</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.zdawn</groupId>
<artifactId>jwt-token-redis</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>js-tokens</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>com.abasecode.opencode</groupId>
<artifactId>abasecode-base-token</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>cn.spark2fire.alberti</groupId>
<artifactId>auth-redis</artifactId>
<version>0.0.10</version>
</dependency>
<dependency>
<groupId>cn.spark2fire.alberti</groupId>
<artifactId>auth-jwt</artifactId>
<version>0.0.10</version>
</dependency>
<dependency>
<groupId>org.moara.yido</groupId>
<artifactId>tokenizer</artifactId>
<version>0.1.2</version>
</dependency>
<dependency>
<groupId>org.webjars.bowergithub.lydell</groupId>
<artifactId>js-tokens</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>cn.spark2fire.alberti</groupId>
<artifactId>auth-core</artifactId>
<version>0.0.10</version>
</dependency>
<dependency>
<groupId>cn.spark2fire.alberti</groupId>
<artifactId>auth-db</artifactId>
<version>0.0.10</version>
</dependency>
<dependency>
<groupId>io.github.fluxroot</groupId>
<artifactId>result</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.github.kittinunf.result</groupId>
<artifactId>result</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>tech.favware</groupId>
<artifactId>result</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.markelliot.result</groupId>
<artifactId>result</artifactId>
<version>0.37.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.project-lombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
public final class WebSecurity extends AbstractConfiguredSecurityBuilder<Filter, WebSecurity>
implements SecurityBuilder<Filter>, ApplicationContextAware {
可以看到WebSecurity继承了AbstractConfiguredSecurityBuilder并实现了SecurityBuilder。
之前分析了解到,WebSecurity由WebSecurityConfiguration创建,以创建称为 Spring Security Filter Chain (springSecurityFilterChain) 的FilterChainProxy 。
6.0以后的版本没有这个类了,需要自己定义一个SecurityFilterChain的Bean,使用HttpSecurity.build()