Ho creato una classe di configurazione Spring Security per Spring-Boot. La mia pagina di login ha risorse css, js e ico files. Le risorse vengono rifiutate per motivi di sicurezza e reindirizzate ogni volta alla pagina di accesso. Perché EnableWebMVCSecurity non aggiunge la posizione della risorsa Classpath. Dopo aver modificato il codice come nel secondo frammento, viene aggiunta la posizione della risorsa I Classpath. non capisco cosa mi manca per le risorse nel primo snippet di codice.Configurazione di sicurezza con Spring-boot
@Configuration
/*
* Enable Spring Security’s web security support and provide the Spring MVC integration
* It also extends WebSecurityConfigurerAdapter
and overrides a couple of its methods to set some specifics of the web security configuration.
*/
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/**
* The configure(HttpSecurity) method defines with URL paths should be
* secured and which should not.
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated();
// There is a custom "/login" page specified by loginPage(), and everyone
// is allowed to view it.
http
.formLogin()
.loginPage("/login.html")
.permitAll()
.and()
.logout()
.permitAll().logoutSuccessUrl("/login.html");
}
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
// As for the configure(AuthenticationManagerBuilder) method, it sets up
// an in-memory user store with a single user. That user is given a
// username of "user", a password of "password", and a role of "USER".
auth
.inMemoryAuthentication()
.withUser("[email protected]").password("password").roles("USER");
}
}
Ho ottenuto questo lavoro modificando il codice di
@Configuration
/*
* Enable Spring Security’s web security support and provide the Spring MVC integration
* It also extends WebSecurityConfigurerAdapter
and overrides a couple of its methods to set some specifics of the web security configuration.
*/
public class WebSecurityConfig{
@Bean
public ApplicationSecurity applicationSecurity() {
return new ApplicationSecurity();
}
@Bean
public AuthenticationSecurity authenticationSecurity() {
return new AuthenticationSecurity();
}
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login.html")
.permitAll()
.and()
.logout()
.permitAll().logoutSuccessUrl("/login.html");
}
}
@Order(Ordered.HIGHEST_PRECEDENCE + 10)
protected static class AuthenticationSecurity extends
GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("[email protected]").password("password").roles("USER");
}
}
}
Dopo aver modificato il codice ho notato che i sentieri Ignora sono stati aggiunti al filtro e vedo quanto segue in ceppi:
[ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/css/**'], [] [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/js/**'], [] [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/images/**'], [] [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/**/favicon.ico'], [] [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: [email protected]1, [org.springframework.secu[email protected]4e3e0069, org.spring[email protected]3d2dd0cf, [email protected]b02, [email protected], org.[email protected]267237ef, org.springframework.s[email protected]129495ef, org.springframework.[email protected]7db0a467, org.springfram[email protected]764d1dbd, org.sp[email protected]25a5268d, org.springframework.[email protected]15c01d0c, org.springfram[email protected]37818a3b, o[email protected]3fe57e49, org[email protected]4278af59, org.springfr[email protected]424bef91]
Grazie per puntatore al documentazione. Ho usato 'EnableWebMVCSecurity' che è diverso da' EnableWebSecurity'. – randominstanceOfLivingThing
È lo stesso (nel senso che è un superset) - uno è annotato con l'altro. –
@DaveSyer, puoi guardare la mia domanda per favore? https://stackoverflow.com/questions/46065063/spring-boot-basic-authentication –