淺談SpringSecurity重寫默認配置
更新時間:2025年01月20日 10:04:56 作者:@來杯咖啡
這篇文章主要介紹了SpringSecurity重寫默認配置,包括注入Bean、擴展WebSecurityConfigurerAdapter、重寫端點授權配置及實現(xiàn)AuthenticationProvider,感興趣的可以了解一下
重寫UserDetailService組件
1.注入Bean的方式
/**
* @author: coffee
* @date: 2024/6/22 21:22
* @description: 重寫springsecurity默認組件:注入Bean的方式
*/
@Configuration
public class ProjectConfig {
/**
* 重寫userDetailsService組件
*/
@Bean
public UserDetailsService userDetailsService () {
// InMemoryUserDetailsManager實現(xiàn)并不適用生成環(huán)境,此處進作為demo使用
InMemoryUserDetailsManager userDetailsService = new InMemoryUserDetailsManager();
// 使用指定用戶名、密碼和權限列表構建用戶
UserDetails user = User.withUsername("john").password("12345").authorities("read").build();
// 添加該用戶以便讓UserDetailsService對其進行管理
userDetailsService.createUser(user);
return userDetailsService;
}
/**
* 重寫UserDetailsService組件也必須重寫PasswordEncoder組件,否則會報:
* java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
*/
@Bean
public PasswordEncoder passwordEncoder () {
// NoOpPasswordEncoder實例會將密碼視為普通文本,他不會對密碼進行加密或者hash處理
return NoOpPasswordEncoder.getInstance();
}
}
2.擴展WebSecurityConfigurerAdapter
/**
* @author: coffee
* @date: 2024/6/22 21:46
* @description:
*/
@Configuration
public class ProjectConfig2 extends WebSecurityConfigurerAdapter {
/**
* 重寫端點授權配置,就需要擴展WebSecurityConfigurerAdapter類,可以使用HttpSecurity對象的不同方法更改配置
*/
@Override
protected void configure (HttpSecurity httpSecurity) throws Exception {
httpSecurity.httpBasic();
// 所有請求都需要身份驗證
// httpSecurity.authorizeRequests().anyRequest().authenticated();
// permitAll()方法修改授權配置,無需憑據(jù)(用戶名密碼)也可以直接調用接口。 curl http://localhost:8080/hello
httpSecurity.authorizeRequests().anyRequest().permitAll();
}
/**
* 重寫springsecurity默認組件:繼承WebSecurityConfigurerAdapter的方式
*/
@Override
protected void configure (AuthenticationManagerBuilder auth) throws Exception {
// InMemoryUserDetailsManager實現(xiàn)并不適用生成環(huán)境,此處進作為demo使用
InMemoryUserDetailsManager userDetailsService = new InMemoryUserDetailsManager();
// 使用指定用戶名、密碼和權限列表構建用戶
UserDetails user = User.withUsername("john").password("12345").authorities("read").build();
// 添加該用戶以便讓UserDetailsService對其進行管理
userDetailsService.createUser(user);
// AuthenticationManagerBuilder調用userDetailsService()方法來注冊UserDetailsService實例
// AuthenticationManagerBuilder調用passwordEncoder()方法來注冊NoOpPasswordEncoder實例
auth.userDetailsService(userDetailsService).passwordEncoder(NoOpPasswordEncoder.getInstance());
}
}重寫端點授權配置
/**
* @author: coffee
* @date: 2024/6/22 21:46
* @description:
*/
@Configuration
public class ProjectConfig2 extends WebSecurityConfigurerAdapter {
/**
* 重寫端點授權配置,就需要擴展WebSecurityConfigurerAdapter類,可以使用HttpSecurity對象的不同方法更改配置
*/
@Override
protected void configure (HttpSecurity httpSecurity) throws Exception {
httpSecurity.httpBasic();
// 所有請求都需要身份驗證
// httpSecurity.authorizeRequests().anyRequest().authenticated();
// permitAll()方法修改授權配置,無需憑據(jù)(用戶名密碼)也可以直接調用接口。 curl http://localhost:8080/hello
httpSecurity.authorizeRequests().anyRequest().permitAll();
}
}重寫AuthenticationProvider實現(xiàn)
/**
* @author: coffee
* @date: 2024/6/22 22:15
* @description: ...
*/
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String userName = authentication.getName();
String password = String.valueOf(authentication.getCredentials());
// 重寫身份驗證提供者,用if else 替換 UserDetailsService和PasswordEncoder
if ("john".equals(userName) && "12345".equals(password)) {
return new UsernamePasswordAuthenticationToken(userName, password, Arrays.asList());
} else {
throw new AuthenticationCredentialsNotFoundException("ERROR");
}
}
@Override
public boolean supports(Class<?> authentication) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
}
}
/**
* @author: coffee
* @date: 2024/6/22 21:46
* @description:
*/
@Configuration
public class ProjectConfig2 extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider customAuthenticationProvider;
/**
* 重寫端點授權配置,就需要擴展WebSecurityConfigurerAdapter類,可以使用HttpSecurity對象的不同方法更改配置
*/
@Override
protected void configure (HttpSecurity httpSecurity) throws Exception {
httpSecurity.httpBasic();
// 所有請求都需要身份驗證
httpSecurity.authorizeRequests().anyRequest().authenticated();
}
/**
* 重寫身份驗證提供者
*/
@Override
protected void configure (AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthenticationProvider);
}
}到此這篇關于淺談SpringSecurity重寫默認配置的文章就介紹到這了,更多相關SpringSecurity重寫默認配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
Java的System.getProperty()方法獲取大全
這篇文章主要介紹了Java的System.getProperty()方法獲取大全,羅列了System.getProperty()方法獲取各類信息的用法,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12
AsyncHttpClient?ClientStats源碼流程解讀
這篇文章主要為大家介紹了AsyncHttpClient?ClientStats源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12

