![](/img/trans.png)
[英]Spring security authentication without spring boot doesn't work as opposed to with spring boot and similar configuration
[英]Spring Boot Security - allow without authentication
我正在尝试配置 Spring Boot Security 以允许匿名用户访问除一个之外的所有 URL。 默认用户和由 Spring 生成的安全密码。 我只需要一页维护应用程序 我已经尝试了很多技巧和教程。
和别的。
但是 Spring 仍然需要对所有页面进行身份验证。
我当前的安全配置
@EnableWebSecurity
@Configuration
public class SecurityConf extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.anyRequest()
.anonymous()
.antMatchers("/secure")
.authenticated();
}
}
网页配置
@Configuration
@EnableWebMvc
@EnableAsync
public class WebConf implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
WebMvcConfigurer.super.addResourceHandlers(registry);
registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/**");
}
@Bean
public Executor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(5);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("Asynchronous Process-");
executor.initialize();
return executor;
}
}
和主要方法
@ComponentScan
@SpringBootApplication
@EnableScheduling
public class MainServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MainServiceApplication.class, args);
}
}
我试过
.permitAll()
.anonymous()
没有成功。
编辑 1
项目结构
编辑 2
@ComponentScan()
@SpringBootApplication
@EnableScheduling
public class MainServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MainServiceApplication.class, args);
}
}
通过移动配置包解决。 Spring 没有扫描配置包。
您可能需要更改顺序。 可能的问题是antMatchers("/secure").authenticated()
无效,因为/secure端点将在anyRequest()
考虑。 还要确保SecurityConf
位于扫描所需的正确包中。
@EnableWebSecurity
@Configuration
public class SecurityConf extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/secure").authenticated()
.anyRequest().anonymous();
}
}
或者
@EnableWebSecurity
@Configuration
public class SecurityConf extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/secure").authenticated()
.anyRequest().permitAll();
}
}
更新
您需要在cz.lh.main_service
创建一个configs包, SecurityConf
和WebConf
应该是cz.lh.main_service.configs
一部分
或者
您可以使用@ComponentScan
并可以指定您拥有SecurityConf
和WebConf
的当前包
@ComponentScan(“your-config-package”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.