Ich versuche, Spring Security-Projekt mit JWT zu entwickeln. Ich möchte Zugriff Anmeldung API ohne Spring Security (ohne JWT-Token). Aber mit unten Konfiguration, jedes Mal (für LOGON API auch) prüft es für JWT Token, das mir Fehler 403 gibt.Spring Security mit JWT
Unten ist meine WebSecurityConfig.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").hasRole("USER").and().formLogin()
.usernameParameter("username") // default is username
.passwordParameter("password") // default is password
.loginPage("/authentication/login") // default is /login with an HTTP get
.failureUrl("/authentication/login?failed") // default is /login?error
.loginProcessingUrl("/authentication/login/process"); // default is /login
// with an HTTP
// post
}
Wenn einige Pfade configure(WebSecurity web)
ignoriert werden müssen, können außer Kraft gesetzt werden:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtAuthFilter jwtAuthFilter;
@Autowired
private TokenAuthenticationService jwtAuthenticationProvider;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(jwtAuthenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/api/v1/login");
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/api/v1/login")
.permitAll()
.and()
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
}
}
Vielen Dank im Voraus
Danke für die Antwort. Gibt es einen sauberen Weg wie (ignorantAntMatchers), so dass ich dies in configure (HttpSecurity http) überprüfen kann? – kedar
Ich bin mir nicht sicher über Ameisenmarschierer, aber falls Sie nur den Anmeldepfad konfigurieren müssen, habe ich die Antwort bearbeitet und dort ein Beispiel aus der Dokumentation hinzugefügt. –