Вот мой WebAppInitializer:Spring Security/j_spring_security_check Не найдено 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[] { "/" };
}
}
Вот мой Security Config:
@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
}
}
И это мой login.html (пример HTML от 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>
Когда я нахожусь на имени пользователя, появляется эта ошибка:
HTTP ERROR 404
Problem accessing /j_spring_security_check. Reason:
Not Found
Как я могу избавиться от этой ошибки? Я много гугл, но пока ничего не добился. (Да, я не использую любой XML.)
Альтернатива вышесказанному заключается в том, чтобы программно инициализировать фильтр. IE: FilterRegistration.Dynamic springSecurityFilterChain = container.addFilter ("springSecurityFilterChain", DelegatingFilterProxy.class); springSecurityFilterChain.addMappingForUrlPatterns (null, false, "/ *"); Для меня этот подход имеет больше смысла или вы можете добавить его в свое приложение xml. То же самое, имея пустой класс, просто кажется неправильным и, вероятно, будет удалено в будущем, если не будет прокомментировано из-за отсутствия ссылок. –