【发布时间】:2015-07-26 08:40:10
【问题描述】:
我是 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 );
}
}
【问题讨论】:
-
尝试从
j_spring_security_check中删除/或使用上下文的绝对路径创建URL 然后/j_spring_security_check。 -
@BretC - 我确实尝试过,它在一定程度上有所帮助,但我很快就被
j_spring_security_logout卡住了。我现在已经更新了答案。 @OO7 - 谢谢,但这也无济于事。
标签: java spring spring-security http-status-code-404 j-security-check