Java 配置 /j_spring_security_check 未找到

Java config /j_spring_security_check not found

我是 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 );
}
}

docs

.loginPage("/") 覆盖您必须提交的 url。

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

这样更改了我的 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 {
}

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

@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();
}

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