Ich habe zwei verschiedene Login-Flows in meiner Spring Boot App.Spring Security mehrere Login-Filter
- Normalform Login
- Stateless Login
habe ich versucht, die folgenden, aber es kommt immer auf den 2. ein Filter. Wie schränke ich /api/**
, um für statuslose Authentifizierung und andere auf normale Sitzung Formular Login zu beschränken?
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select username,password, enabled from users where username=?")
.authoritiesByUsernameQuery("select username, authority from authorities where username=?");
}
private final UserService userService;
private final TokenAuthenticationService tokenAuthenticationService;
public WebSecurityConfig() {
super(true);
this.userService = new UserService();
tokenAuthenticationService = new TokenAuthenticationService("tooManySecrets", userService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable();
// Normal form login using jdbc
http
.authorizeRequests()
.antMatchers("/", "/home").access("hasRole('ROLE_ADMIN')")
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("username")
.passwordParameter("password")
.permitAll()
.and()
.logout().permitAll();
//Stateless authentication using tokens - Custome authentication from token implemented in the filter
http
.authorizeRequests()
.antMatchers("/api").authenticated()
.and()
.addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService),
UsernamePasswordAuthenticationFilter.class);
}
}