Spring启动和Spring Security inMemoryAuthentication无法正常工作

时间:2015-10-16 13:42:38

标签: java spring spring-security spring-boot

我正在尝试使用Spring安全性为应用程序配置Spring启动。

然而,inMemoryAuthentication()似乎无法正常工作,并且我的每个用户都有以下错误:

INFO 6964 --- [nio-8080-exec-6] o.s.b.a.audit.listener.AuditListener     : AuditEvent [timestamp=Fri Oct 16 19:09:41 IST 2015, principal=anonymousUser, type=AUTHORIZATION_FAILURE, data={message=Access is denied, type=org.springframework.security.access.AccessDeniedException}]

以下是使用的文件配置:

SecurityConfig.java:

@Configuration
@EnableWebMvcSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    public void configure( WebSecurity web ) throws Exception
    {
        //
        web
        .ignoring()
        .antMatchers( "/WEB-INF/jsp/**" );
    }

    @Autowired
    public void configureGlobal( AuthenticationManagerBuilder auth ) throws Exception
    {
          auth.inMemoryAuthentication()
                    .withUser("user").password("password").roles("USER");


    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http

            .authorizeRequests().antMatchers("/homepage**").hasRole("USER")
            .antMatchers("/index**").permitAll()
            .antMatchers("/login**").permitAll()

            .and()
            .formLogin()
            .loginPage( "/login" )
            .loginProcessingUrl( "/login" )
            .defaultSuccessUrl( "/index" );
        http.csrf().disable();

    }

即使应用程序无法使用给定的"用户"和"密码&#34 ;;

Spring boot version: 1.2.6
Editor: STS

参考Debug日志,inMemoryAuthencated用户显示为匿名用户:

org.springframework.security.authentication.AnonymousAuthenticationToken@90576bf4: **Principal: anonymousUser**; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@21a2c: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: FB03A4C352FC0164A5A3F751E52A5421; **Granted Authorities: ROLE_ANONYMOUS**

有什么见解?

1 个答案:

答案 0 :(得分:2)

如果您使用自动生成的登录页面替换自定义登录过程:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/homepage**").hasRole("USER")
        .antMatchers("/index**").permitAll()
        .antMatchers("/login**").permitAll()
        .and()
        .formLogin();
        //.loginPage( "/login" )
        //.loginProcessingUrl( "/login" )
        //.defaultSuccessUrl( "/index" );
    http.csrf().disable();
}

错误是否仍然存在?如果没有,这似乎是您的自定义登录过程的问题。