gpt4 book ai didi

java - WebSecurityConfigurerAdapter 不起作用

转载 作者:行者123 更新时间:2023-12-01 09:20:36 26 4
gpt4 key购买 nike

我尝试将 Spring Security 添加到我的项目中,但没有成功。我仍然可以在没有任何登录的情况下访问我的网络应用程序。

主要配置

public class SpittrWebAppInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}

@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class,SecurityConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfig.class};
}
}

安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().and()
.httpBasic();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}

我做错了什么?使用SpringSecurity 4.1.3

最佳答案

要启用 Spring Security 需要添加这样的类

public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
}

或者xml配置

    <filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

关于java - WebSecurityConfigurerAdapter 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40189600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com