Spring Security настроен следующим образом:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**", "/images/**", "/js/login.js", "/lib/**", "/fonts/**")
.permitAll()
.antMatchers(
"/api")
.access("hasAnyRole('ROLE_ADMIN', 'ROLE_READ-ONLY')")
.anyRequest()
.access("hasRole('ROLE_ADMIN')")
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.csrf()
.ignoringAntMatchers("/actuator/**")
.and()
.exceptionHandling()
.authenticationEntryPoint(entryPoint())
.and()
.formLogin()
.defaultSuccessUrl("/index", true);
}
Я настроил через контроллер представления логин. html Страница, которая отображается при вызове / входе в систему:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login");
}
}
Я пробовал это решение , но, похоже, оно не работает для Spring Security 5 (или, по крайней мере, для моей конфигурации). Когда я звоню:
curl --cookie-jar cookie -L http://localhost:9002/login | grep csrf
, я получаю:
<meta name="_csrf" content="HexValueWithHyphens" />
<meta name="_csrf_header" content="X-CSRF-TOKEN" />
<form action="/logout" method="post"><input type="hidden" name="_csrf" value="HexValueWithHyphens"/>
<form action="/login" method="POST"><input type="hidden" name="_csrf" value="HexValueWithHyphens"/>
Когда я слежу за звонком с curl --cookie cookie -d "username=myuser&password=somePassEndingWithTwoAtSymbols%40%40&_csrf=HexValueWithHyphens" -L http://localhost:9002/login
, я получаю содержимое страницы входа HTML. При любом последующем обращении к защищенному ресурсу также возвращается логин. html page.