55 lines
2.2 KiB
Java
55 lines
2.2 KiB
Java
package com.kob.backend.config;
|
|
|
|
/*
|
|
主要作用:放行登录、注册等接口
|
|
*/
|
|
|
|
import com.kob.backend.config.filter.JwtAuthenticationTokenFilter;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.http.HttpMethod;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
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.crypto.bcrypt.BCryptPasswordEncoder;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
|
// TODO: 2023/2/19 WebSecurityConfigurerAdapter 已被弃用,注意替换
|
|
@Configuration
|
|
@EnableWebSecurity
|
|
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|
@Autowired
|
|
private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
|
|
|
|
// 配置密码加密方式
|
|
@Bean
|
|
public PasswordEncoder passwordEncoder() {
|
|
return new BCryptPasswordEncoder();
|
|
}
|
|
|
|
@Bean
|
|
@Override
|
|
public AuthenticationManager authenticationManagerBean() throws Exception {
|
|
return super.authenticationManagerBean();
|
|
}
|
|
/*
|
|
.antMatchers("/user/account/token/", "/user/account/register/").permitAll()
|
|
用于配置公开链接
|
|
*/
|
|
@Override
|
|
protected void configure(HttpSecurity http) throws Exception {
|
|
http.csrf().disable()
|
|
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
|
.and()
|
|
.authorizeRequests()
|
|
.antMatchers("/user/account/token/", "/user/account/register/").permitAll()
|
|
.antMatchers(HttpMethod.OPTIONS).permitAll()
|
|
.anyRequest().authenticated();
|
|
|
|
http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
|
|
}
|
|
} |