Spring-Security取消验证-Get请求接口正常,Post请求报错403

现象:springcloud项目夹带security相关的包后启动提示如下:

Using generated security password: f67f9afa-xxxxxxxxxxx-a85e-88175f5a7c8a

This generated password is for development use only. Your security configuration must be updated before running your application in production.

一眼看上去项目中带有Spring-Security相关包,经过maven依赖查看后加了exclude排除,但是问题依然存在:

        Get请求接口正常,Post请求报错403.

于是乎加入WebSecurityConfig才屏蔽了验证,代码如下:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll().and().csrf().disable();
    }
}

通常,禁用spring security两种方法:

@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})
或者

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security 默认情况下只支持 POST 方法提交登录数据。要支持 GET 方法提交登录数据,需要通过配置 `org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter` 过滤器的 `setPostOnly(false)` 方法来实现。 具体实现步骤如下: 1. 创建一个继承自 `UsernamePasswordAuthenticationFilter` 的类,重写其 `attemptAuthentication` 方法。 ```java public class CustomUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter { @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { if (!"POST".equals(request.getMethod()) && !"GET".equals(request.getMethod())) { throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); } // 处理登录请求 // ... } } ``` 2. 在 Spring Security 配置类中,将上述自定义的过滤器添加到过滤器链中,并设置 `setPostOnly(false)`。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomUserDetailsService userDetailsService; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .loginProcessingUrl("/login") .and() .logout() .logoutUrl("/logout") .and() .csrf().disable() .addFilterAt(customUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); } @Bean public CustomUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter() throws Exception { CustomUsernamePasswordAuthenticationFilter filter = new CustomUsernamePasswordAuthenticationFilter(); filter.setAuthenticationManager(authenticationManagerBean()); filter.setFilterProcessesUrl("/login"); filter.setPostOnly(false); return filter; } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } } ``` 这样就可以通过 GET 请求提交登录数据了。但是,由于 GET 请求会将参数暴露在 URL 中,存在安全风险,建议还是使用 POST 请求

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值