【问题标题】:No Authentication and authorization using Spring Security没有使用 Spring Security 的身份验证和授权
【发布时间】:2015-05-22 04:57:51
【问题描述】:

我的项目要求我使用 Spring Security 进行 CSRF 和 XSS 保护,但不使用它进行身份验证和授权。我已经在我的应用程序中配置了 SS,但是每次我访问一个页面时,它都会自动将我重定向到它的登录页面。如何禁用此功能?我的SecurityConfig 文件是:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
            throws Exception {
    }
}

【问题讨论】:

    标签: java spring authentication spring-security authorization


    【解决方案1】:

    如下配置 spring 安全配置以及所需的 spring 安全依赖项。自行测试以确保它满足您的所有需求。

    package org.springframework.security.samples.config;
    
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().antMatchers("**").permitAll().and().csrf()
                    .and().headers().frameOptions().sameOrigin().xssProtection();
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      下面给出的SecurityConfig 将允许所有请求不经过身份验证,但将具有 CSRF 和 XSS 保护:

      @EnableWebSecurity
      public class SecurityConfig extends WebSecurityConfigurerAdapter {
      
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http.authorizeRequests().anyRequest().permitAll();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-05-10
        • 2011-07-17
        • 2010-11-22
        • 2020-03-15
        • 2014-05-11
        • 1970-01-01
        • 2021-02-24
        • 2016-08-15
        • 2021-03-19
        相关资源
        最近更新 更多