不使用AbstractSecurityWebApplicationInitializer启用Spring Security
创始人
2024-12-28 09:30:07
0

要在不使用AbstractSecurityWebApplicationInitializer的情况下启用Spring Security,可以通过以下步骤进行设置:

  1. 首先,确保你的项目中已经添加了Spring Security的依赖。可以在pom.xml文件中添加以下依赖:

    org.springframework.boot
    spring-boot-starter-security

  1. 创建一个类用于配置Spring Security。可以创建一个名为SecurityConfig的类,并将其标记为@Configuration。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/public/**").permitAll() // 公共路径不需要认证
            .anyRequest().authenticated() // 其他路径需要认证
            .and()
            .formLogin().permitAll() // 开启表单登录
            .and()
            .logout().permitAll(); // 开启注销功能
    }
    
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("user").password("{noop}password").roles("USER"); // 在内存中配置一个用户
    }
}
  1. 这里示例中使用了基于表单的认证,配置了一个公共路径和一个需要认证的路径。在configure(AuthenticationManagerBuilder auth)方法中,使用了内存认证,配置了一个用户名为"user",密码为"password"的用户。

  2. 如果你使用的是Spring Boot,那么以上配置已经足够启用Spring Security。如果你使用的是传统的Spring MVC项目,还需要在web.xml文件中添加一个DelegatingFilterProxy过滤器。


    springSecurityFilterChain
    org.springframework.web.filter.DelegatingFilterProxy



    springSecurityFilterChain
    /*

以上就是在不使用AbstractSecurityWebApplicationInitializer的情况下启用Spring Security的解决方法。你可以根据自己的需要进行配置和调整。

相关内容

热门资讯

透视玄学!xpoker辅助怎么... 透视玄学!xpoker辅助怎么用,一向真的是有挂(透视)攻略教程(有挂插件);在进入xpoker辅助...
透视最新!wepoker透视方... 透视最新!wepoker透视方法,竟然是有挂(透视)wpk教程(有挂详情)1、wepoker透视方法...
透视能赢!pokermaste... 透视能赢!pokermaster辅助器,从前真的是有挂(透视)2025新版(有挂黑科技)1、操作简单...
透视黑科技!哈糖大菠萝开挂,竟... 透视黑科技!哈糖大菠萝开挂,竟然有挂(透视)靠谱教程(有挂细节)1、下载好哈糖大菠萝开挂辅助软件之后...
透视科技!wepoker私人局... 透视科技!wepoker私人局透视方法,都是真的有挂(透视)新2025教程(有挂方法)1、wepok...
透视黑科技!hhpoker哪个... 透视黑科技!hhpoker哪个俱乐部靠谱,都是真的有挂(透视)爆料教程(有挂攻略);1、构建自己的h...
透视app!pokemmo手机... 透视app!pokemmo手机脚本辅助器,竟然是有挂(透视)新2025版(有挂教程)1、pokemm...
透视脚本!wpk辅助哪里买,一... 透视脚本!wpk辅助哪里买,一贯存在有挂(透视)普及教程(有挂详情)1、wpk辅助哪里买系统规律教程...
透视安卓版!wepoker破解... 透视安卓版!wepoker破解器,本来真的有挂(透视)AI教程(有挂方法)1、操作简单,无需注册,只...
透视新版!wepoker怎么设... 透视新版!wepoker怎么设置房间,好像真的是有挂(透视)扑克教程(有挂插件);1、用户打开应用后...