找不到Java config / j_spring_security_check

时间:2015-05-14 16:20:47

标签: java spring spring-security http-status-code-404 j-security-check

我是Spring框架的菜鸟。

尝试为应用配置安全选项。我跟随我的(xml-less)安全配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("accountService")
UserDetailsService userDetailsService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}

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

    http.csrf().disable().authorizeRequests()
            .antMatchers("/admin/**" ).hasRole( "Admin" )
            .and()
            .formLogin()
            .loginPage("/")
            .loginProcessingUrl( "/j_spring_security_check" )
            .failureUrl( "/loginfailed" )
            .permitAll()
            .and().logout().logoutSuccessUrl("/logout")
            .and().exceptionHandling().accessDeniedPage("/403");
}

@Bean
public PasswordEncoder passwordEncoder(){
    PasswordEncoder encoder = new BCryptPasswordEncoder();
    return encoder;
}

}

它显示登录页面,但是当我提交它时会抛出/j_spring_security_check Not Found异常。非常感谢任何帮助。

我的网络配置是:

public class WebConfig implements WebApplicationInitializer {

public void onStartup( ServletContext servletContext ) throws ServletException {

    AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
    applicationContext.register( MvcServletConfig.class );
    applicationContext.register( SecurityConfig.class );

    //Add the servlet mapping manually and make it initialize automatically
    ServletRegistration.Dynamic servlet = servletContext.addServlet( "dispatcher", new DispatcherServlet( applicationContext ) );
    servletContext.addListener(new ContextLoaderListener(applicationContext));
    servlet.addMapping( "/" );
    servlet.setLoadOnStartup( 1 );
}
}

2 个答案:

答案 0 :(得分:0)

请参阅docs

.loginPage("/")会覆盖您必须提交的网址。

你想要的可能是.loginPage("/j_spring_security_check")

答案 1 :(得分:0)

改变了我的WebConfig

@Configuration
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
    return new String[ ] { "/" };
}

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[ ] { AppConfig.class, SecurityConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[ ]{ MvcServletConfig.class };
}
}

添加AbstractSecurityWebApplicationInitializer如下:

public class SecurityInitialiser extends AbstractSecurityWebApplicationInitializer {
}

我在configure()中的SecurityConfig方法覆盖现在看起来像这样:

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

    http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin()
                .loginPage( "/" )
                .loginProcessingUrl( "/j_spring_security_check" )
                .defaultSuccessUrl( "/hello" )
                .failureUrl( "/loginfailed" )
                .permitAll()
            .and()
            .logout()
                .logoutUrl( "/j_spring_security_logout" )
                .logoutSuccessUrl( "/" )
                .invalidateHttpSession( true )
            .and()
            .exceptionHandling().accessDeniedPage( "/WEB-INF/pages/403.jsp" )
            .and()
            .csrf()
            .and()
            .httpBasic();
}

希望这有助于某人!感谢所有提出建议的人。 : - )