spring security2.7
时间: 2023-09-04 09:11:18 浏览: 96
在Spring Security 2.7及以上版本中,WebSecurityConfigurerAdapter已经被废弃。相反,现在推荐使用bean注入的方式进行配置。你可以参考官方文档:https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter#ldap-authentication 来了解更多详细信息。在SecurityConfig中,加载SpringSecurityAdminConfig即可。你可以使用@EnableWebSecurity注解来启用Web安全配置。在SecurityConfig类中,通过@Resource注解注入SpringSecurityAdminConfig,并通过@Bean注解创建SecurityFilterChain bean。在configure(HttpSecurity http)方法中,你可以通过以下代码配置安全性:
```
http
.csrf(AbstractHttpConfigurer::disable) //禁用CSRF保护
.sessionManagement(AbstractHttpConfigurer::disable) //禁用Session管理
.authorizeHttpRequests(auhtor-> auhtor
.antMatchers("/admin/code","/admin/login").permitAll() //设置允许匿名访问的URL
.anyRequest().authenticated() //其余请求需要认证
);
http.apply(springSecurityAdminConfig); //应用SpringSecurityAdminConfig配置
return http.build();
```
然后,你需要创建一个名为SpringSecurityAdminConfig的配置类,该类用于管理后台的用户名和密码登录。你可以参考以下示例代码:
```
@Component
public class SpringSecurityAdminConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
@Override
public void configure(HttpSecurity http) throws Exception {
AdminUsernamePasswordAuthenticationFilter adminUsernamePasswordAuthenticationFilter = new AdminUsernamePasswordAuthenticationFilter();
adminUsernamePasswordAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
http.addFilterAt(adminUsernamePasswordAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); //替换UsernamePasswordAuthenticationFilter
}
}
```
这样就可以通过bean注入的方式配置Spring Security 2.7及以上版本了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [springboot2.7整合springSecurity](https://blog.csdn.net/gzmyh/article/details/129267449)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文