如何在Spring Boot应用中关闭Spring Security

23 浏览
0 Comments

如何在Spring Boot应用中关闭Spring Security

我已经在我的Spring Boot应用程序中使用Spring Security实现了身份验证。\n控制身份验证的主要类应该是websecurityconfig:\n@Configuration\n@EnableWebSecurity\n@PropertySource(value = { \"classpath:/config/application.properties\" })\npublic class WebSecurityConfig extends WebSecurityConfigurerAdapter {\n @Autowired\n private RestAuthenticationSuccessHandler authenticationSuccessHandler;\n @Autowired\n private RestAuthenticationEntryPoint restAuthenticationEntryPoint;\n @Override\n protected void configure(HttpSecurity http) throws Exception {\n http\n .httpBasic()\n .and()\n .csrf().disable()\n .sessionManagement().sessionCreationPolicy(\n SessionCreationPolicy.STATELESS)\n .and()\n .exceptionHandling()\n .authenticationEntryPoint(restAuthenticationEntryPoint)\n .and()\n .authorizeRequests()\n .antMatchers(\"/\").permitAll()\n .antMatchers(\"/login\").permitAll()\n .antMatchers(\"/logout\").permitAll()\n .antMatchers(\"/ristore/**\").authenticated()\n .anyRequest().authenticated()\n .and()\n .formLogin()\n .successHandler(authenticationSuccessHandler)\n .failureHandler(new SimpleUrlAuthenticationFailureHandler());\n }\n}\n由于我正在进行OAuth,我还有AuthServerConfig和ResourceServerConfig。我的主要应用程序类如下:\n@SpringBootApplication\n@EnableSpringDataWebSupport\n@EntityScan({\"org.mdacc.ristore.fm.models\"}) \npublic class RistoreWebApplication extends SpringBootServletInitializer\n{\n @Bean\n public WebMvcConfigurer corsConfigurer() {\n return new WebMvcConfigurerAdapter() {\n @Override\n public void addCorsMappings(CorsRegistry registry) {\n registry.addMapping(\"/**\").allowedOrigins(\"*\");\n }\n };\n }\n public static void main( String[] args )\n {\n SpringApplication.run(RistoreWebApplication.class, args);\n }\n protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {\n return application.sources(RistoreWebApplication.class);\n }\n}\n由于我们正在进行代码整合,我们需要暂时关闭身份验证。但是,我尝试了以下方法,似乎没有任何方法有效。当我访问这些rest api url时,我仍然得到401错误。\n1. 注释掉与安全相关的所有类中的注释,包括@Configuration,@EnableWebSecurity。在Spring boot Security Disable security中,建议在底部添加@EnableWebSecurity将禁用认证,我认为这没有任何意义。无论如何尝试,都没有起作用。\n2. 通过删除所有安全相关的内容并只执行以下操作修改websecurityconfig:\nhttp\n .authorizeRequests()\n .anyRequest().permitAll();\n3. 移除安全自动配置@EnableAutoConfiguration(exclude = {\n org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,\n org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration.class}),就像在disabling spring security in spring boot app中所做的那样。但是我认为这个功能只适用于我没有的spring-boot-actuator。所以我没有尝试这个。\n如何正确禁用spring security?

0
0 Comments

在Spring Boot应用程序中关闭Spring Security的方法是创建一个WebFilter并允许所有请求的交换,并禁用CSRF。只需将以下代码放入应用程序的类中即可:

public class ConverterApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConverterApplication.class, args);
    }
    
    public SecurityWebFilterChain chain(ServerHttpSecurity http, AuthenticationWebFilter webFilter) {
        return http.authorizeExchange().anyExchange().permitAll().and()
                .csrf().disable()
                .build();
    }
}

这样做将完美运行。

0
0 Comments

如何在Spring Boot应用程序中关闭Spring Security

在某些情况下,您可能需要临时禁用Spring Security来测试或调试您的应用程序。下面是一些方法可以帮助您关闭Spring Security。

方法1:注释掉安全配置中的注解

//

方法2:在安全配置中添加以下代码

spring.security.enabled=false

management.security.enabled=false

security.basic.enabled=false

通过采用这些方法之一,您可以在Spring Boot应用程序中关闭Spring Security。希望这些方法对您有所帮助!

0
0 Comments

如何在Spring Boot应用程序中关闭Spring Security

在Spring Boot应用程序中,有时候需要临时关闭Spring Security。下面是原因和解决方法的整理。

原因:

有时候,我们可能需要在开发或测试阶段临时关闭Spring Security,以便我们可以更方便地进行调试和测试。

解决方法:

为了在Spring Boot主类中关闭Spring Security,我们可以添加以下代码:

(exclude = org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class)

public class MainClass {

这将排除自动配置类org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,从而关闭Spring Security。

此外,还需要确保在SecurityConfig类中注释掉相关代码,如下所示:

// @EnableWebSecurity

// public class SecurityConfig extends WebSecurityConfigurerAdapter {

// ...

// }

以上操作将关闭Spring Security,使我们可以在开发或测试阶段更轻松地运行和调试我们的应用程序。

在Spring Boot应用程序中,关闭Spring Security有时是必要的。通过在主类中排除自动配置类org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,并注释掉SecurityConfig类中的相关代码,我们可以实现关闭Spring Security的目的。这样做将使我们能够更方便地进行开发和测试。

0