Đâ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.)
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ó. –