2014-12-14 20 views
7

Đây là WebAppInitializer tôi:Xuân An/j_spring_security_check Not Found 404

@Configuration 
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

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

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

    @Override 
    protected String[] getServletMappings() { 
     return new String[] { "/" }; 
    } 
} 

Đây là Config an tôi:

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private UserDetailsService userDetailsService; 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService); 
    } 

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

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http.authorizeRequests() 
       .antMatchers("/signup", "/about").permitAll() 
       .antMatchers("/admin/**").hasRole("ADMIN") 
       .anyRequest().authenticated() 
       .and().formLogin().loginPage("/login").defaultSuccessUrl("/home").failureUrl("/error").permitAll() 
       .and().httpBasic(); 
     // @formatter:on 
    } 

} 

Và đây là login.html tôi (ví dụ html từ thymeleaf):

<form th:action="@{/j_spring_security_check}" method="post"> 
    <label for="j_username">Username</label>: 
    <input type="text" id="j_username" name="j_username" /> <br /> 

    <label for="j_password">Password</label>: 
    <input type="password" id="j_password" name="j_password" /> <br /> 

    <input type="submit" value="Log in" /> 
</form> 

Khi tôi nhấp vào đăng nhập, lỗi này xuất hiện:

HTTP ERROR 404 

Problem accessing /j_spring_security_check. Reason: 

    Not Found 

Tôi làm cách nào để loại bỏ lỗi này? Tôi googled rất nhiều, nhưng không thành công được nêu ra. (Vâng, tôi không sử dụng bất kỳ xml.)

Trả lời

9

Tôi đã tạo ra điều này, và nó hoạt động bây giờ:

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; 

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { 

} 

này kích hoạt các SpringSecurityFilterChain apperently.

Tôi cũng phải chuyển từ @EnableWebSecurity thành @EnableWebMvcSecurity do lỗi CSRF. Như trong tài liệu mùa xuân được viết:

... Lý do bảo mật mùa xuân đang bảo vệ chống lại CSRF attakcks và không có mã thông báo CSRF bao gồm trong yêu cầu của chúng tôi. Cập nhật cấu hình của chúng tôi để sử dụng chú thích @EnableWebMvcSecurity sẽ làm tương tự như @EnableWebMvcSecurity và cung cấp tích hợp với Spring MVC. Trong số những thứ khác, nó sẽ đảm bảo CSRF Mã của chúng tôi là bao gồm các hình thức của chúng tôi tự động khi sử dụng Thymleaf 2.1 + hoặc Spring MVC taglibs ...

Cũng cám ơn M. Deinum cho bình luận của mình. Gần đây, Spring đã chuyển tên/j_spring_security_check/j_password/j_username thành/login/password/username.

+0

Cách thay thế ở trên là khởi tạo bộ lọc theo chương trình. IE: FilterRegistration.Dynamic springSecurityFilterChain = container.addFilter ("springSecurityFilterChain", DelegatingFilterProxy.class); springSecurityFilterChain.addMappingForUrlPatterns (null, false, "/ *"); Với tôi cách tiếp cận này có ý nghĩa hơn hoặc bạn có thể thêm nó vào ứng dụng của bạn xml. Có điều tương tự, có một lớp học trống dường như sai và có thể sẽ bị xóa trong tương lai nếu không nhận xét do không có tài liệu tham khảo hiện có. –

18

Có thể phiên bản vấn đề nếu bạn đang sử dụng 4.X mùa xuân sau đó

a.) Url login =/login b.) Logout url =/logout

nơi như trong trường hợp mùa xuân 3.x

a.) url login =/j_spring_security_check b.) logout url =/j_spring_security_logout

3

Tôi cũng phải đối mặt với vấn đề này khi tắt csrf. Tôi đã cố gắng chỉ định rõ ràng loginProcessingUrl và nó hoạt động hoàn hảo!

@Override 
protected void configure(HttpSecurity httpSecurity) throws Exception { 
    httpSecurity.csrf().disable(); 
    httpSecurity.formLogin().loginProcessingUrl("/login-url"); 
} 

Xin lưu ý rằng:

  1. Url này chỉ cho phép HTTP POST phương pháp.
  2. mặc định url quan trọng khác trong Spring Security là: 1. /login: trả lại mẫu đăng nhập mặc định 2. /logout

phiên bản My Spring Security là 4.0.3.RELEASE

P/S: Câu trả lời của tôi có thể không liên quan trực tiếp cho câu hỏi nhưng tôi nghĩ rằng nó có thể giúp một người mới đến Spring Security như tôi