Spring Security configuration

This is a very important configuration example.

We will create an AuthenticationProvider bean. We will be using our custom LoginModule and then use org.springframework.security.authentication.jaas.DefaultJaasAuthenticationProvider to set things up. We then set this authentication provider as the global provider. Any request will pass through this provider (SpringSecurityConfig.java):

@Bean
DefaultJaasAuthenticationProvider jaasAuthenticationProvider() {
AppConfigurationEntry appConfig = new AppConfigurationEntry("com.packtpub.book.ch04.springsecurity.loginmodule.JaasLoginModule",
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, new HashMap());

InMemoryConfiguration memoryConfig = new InMemoryConfiguration(new AppConfigurationEntry[] { appConfig });

DefaultJaasAuthenticationProvider def = new DefaultJaasAuthenticationProvider();
def.setConfiguration(memoryConfig);
def.setAuthorityGranters(new AuthorityGranter[] {jaasAuthorityGranter});
return def;
}

//We are configuring jaasAuthenticationProvider as our global AuthenticationProvider
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(jaasAuthenticationProvider());
}

The next most important method is the configure method, in which we will make sure that we set the right path which need to be secured and we will also set up some important configurations:

// Setting up our HTTP security
@Override
protected void configure(HttpSecurity http) throws Exception {

// Setting up security
http.authorizeRequests()
.regexMatchers("/admin/.*").hasRole("ADMIN")
.anyRequest().authenticated().and().httpBasic();

// Setting our login page and to make it public
http.formLogin().loginPage("/login").permitAll();
// Logout configuration
http.logout().logoutSuccessUrl("/");
// Exception handling, for access denied
http.exceptionHandling().accessDeniedPage("/noaccess");
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.168.46