【问题标题】:need spring security java config example showing basic auth only需要仅显示基本身份验证的 Spring Security Java 配置示例
【发布时间】:2013-07-07 16:44:21
【问题描述】:

我当前的 java 安全配置如下:

@Configuration
@EnableWebSecurity
public class RootConfig extends WebSecurityConfigurerAdapter {

@Override
protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception
{
auth.inMemoryAuthentication()
    .withUser("tester").password("passwd").roles("USER");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
     http
     .authorizeUrls()
         .anyRequest().authenticated()
         .and()
     .httpBasic();       
  }
}

当我使用浏览器执行 GET 请求时,我会收到错误 403。 我希望得到一个浏览器弹出窗口,要求我输入用户名/密码。 可能是什么问题?

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    更新:这已在 Spring Security 3.2.0.RC1+ 中修复

    这是安全 Java 配置中的一个错误,将在下一个版本中解决。我创建了SEC-2198 来跟踪它。目前,一种解决方法是使用如下内容:

    @Bean
    public BasicAuthenticationEntryPoint entryPoint() {
        BasicAuthenticationEntryPoint basicAuthEntryPoint = new BasicAuthenticationEntryPoint();
        basicAuthEntryPoint.setRealmName("My Realm");
        return basicAuthEntryPoint;
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    
        http
            .exceptionHandling()
                .authenticationEntryPoint(entryPoint())
                .and()
            .authorizeUrls()
                .anyRequest().authenticated()
                .and()
            .httpBasic();       
    }
    

    PS:感谢您试用 Spring Security Java Configuration!保持反馈:)

    【讨论】:

      【解决方案2】:

      使用 Spring Security 4.2.3 并且可能在您可以简单地使用此配置之前:

      @Configuration
      @EnableWebSecurity
      public class CommonWebSecurityConfig extends WebSecurityConfigurerAdapter {
      
         @Override
         protected void configure(final HttpSecurity http) throws Exception {
           http
              .authorizeRequests()
                 .anyRequest().authenticated()
                 .and()
              .httpBasic();
        }
        @Autowired
        public void dlcmlUserDetails(final AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                .withUser("tom").password("111").roles("USER");
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-10-27
        • 2012-05-08
        • 1970-01-01
        • 2014-11-21
        • 2018-06-23
        • 2011-02-11
        • 2016-03-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多