Loading pom.xml +4 −0 Original line number Diff line number Diff line Loading @@ -39,6 +39,10 @@ <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-core</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> Loading src/main/java/com/example/demo/CustomLoginSuccessHandler.java 0 → 100644 +50 −0 Original line number Diff line number Diff line package com.example.demo; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Configuration; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.web.DefaultRedirectStrategy; import org.springframework.security.web.RedirectStrategy; import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; @Configuration public class CustomLoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler{ @Override protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { String targetUrl = determineTargetUrl(authentication); if (response.isCommitted()) { return; } RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); redirectStrategy.sendRedirect(request, response, targetUrl); } protected String determineTargetUrl(Authentication authentication) { String url = "/login?error=true"; // Fetch the roles from Authentication object Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); List<String> roles = new ArrayList<String>(); for (GrantedAuthority a : authorities) { roles.add(a.getAuthority()); } // check user role and decide the redirect URL if (roles.contains("ADMIN")) { url = "/admin"; } else if (roles.contains("POULAIN") || roles.contains("MENTOR")) { url = "/member"; } return url; } } src/main/java/com/example/demo/HelpRequestController.java +9 −8 Original line number Diff line number Diff line package com.example.demo; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; Loading src/main/java/com/example/demo/MVCController.java +1 −0 Original line number Diff line number Diff line Loading @@ -9,4 +9,5 @@ public class MVCController implements WebMvcConfigurer { public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login"); } } src/main/java/com/example/demo/WebSecurityConfig.java +40 −11 Original line number Diff line number Diff line package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomLoginSuccessHandler successHandler; @Qualifier("userDetailsServiceImpl") @Autowired private UserDetailsService userDetailsService; @Bean public BCryptPasswordEncoder bCryptPasswordEncoder() { return new BCryptPasswordEncoder(); } @Bean public AuthenticationManager customAuthenticationManager() throws Exception { return authenticationManager(); } @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/", "/home").permitAll() .antMatchers("/", "/index").permitAll() .antMatchers("/allMentor").permitAll() .antMatchers("/formMentor").permitAll() .antMatchers("/insertMentor").permitAll() Loading @@ -22,22 +46,27 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { .antMatchers("/formPoulain").permitAll() .antMatchers("/insertPoulain").permitAll() .antMatchers("/allHelpRequest").permitAll() .antMatchers("/formHelpRequest").permitAll() .antMatchers("/formHelpRequest").hasAnyAuthority("POULAIN") .antMatchers("/login").permitAll() .antMatchers("/register").permitAll() .antMatchers("/admin/**").hasAnyAuthority("ADMIN") .antMatchers("/member/**").hasAnyAuthority("POULAIN", "MENTOR") .and() .formLogin() .loginPage("/login") .permitAll() .loginPage("/login").permitAll() .usernameParameter("email") .successHandler(successHandler) .and() .logout() .permitAll(); .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) .logoutSuccessUrl("/").and() .exceptionHandling() .accessDeniedPage("/access-denied"); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("mentor").password("{noop}mentor").roles("MENTOR") .and().withUser("poulain").password("{noop}poulain").roles("POULAIN"); auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder()); } } No newline at end of file Loading
pom.xml +4 −0 Original line number Diff line number Diff line Loading @@ -39,6 +39,10 @@ <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-core</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> Loading
src/main/java/com/example/demo/CustomLoginSuccessHandler.java 0 → 100644 +50 −0 Original line number Diff line number Diff line package com.example.demo; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Configuration; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.web.DefaultRedirectStrategy; import org.springframework.security.web.RedirectStrategy; import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; @Configuration public class CustomLoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler{ @Override protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { String targetUrl = determineTargetUrl(authentication); if (response.isCommitted()) { return; } RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); redirectStrategy.sendRedirect(request, response, targetUrl); } protected String determineTargetUrl(Authentication authentication) { String url = "/login?error=true"; // Fetch the roles from Authentication object Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); List<String> roles = new ArrayList<String>(); for (GrantedAuthority a : authorities) { roles.add(a.getAuthority()); } // check user role and decide the redirect URL if (roles.contains("ADMIN")) { url = "/admin"; } else if (roles.contains("POULAIN") || roles.contains("MENTOR")) { url = "/member"; } return url; } }
src/main/java/com/example/demo/HelpRequestController.java +9 −8 Original line number Diff line number Diff line package com.example.demo; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; Loading
src/main/java/com/example/demo/MVCController.java +1 −0 Original line number Diff line number Diff line Loading @@ -9,4 +9,5 @@ public class MVCController implements WebMvcConfigurer { public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login"); } }
src/main/java/com/example/demo/WebSecurityConfig.java +40 −11 Original line number Diff line number Diff line package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomLoginSuccessHandler successHandler; @Qualifier("userDetailsServiceImpl") @Autowired private UserDetailsService userDetailsService; @Bean public BCryptPasswordEncoder bCryptPasswordEncoder() { return new BCryptPasswordEncoder(); } @Bean public AuthenticationManager customAuthenticationManager() throws Exception { return authenticationManager(); } @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/", "/home").permitAll() .antMatchers("/", "/index").permitAll() .antMatchers("/allMentor").permitAll() .antMatchers("/formMentor").permitAll() .antMatchers("/insertMentor").permitAll() Loading @@ -22,22 +46,27 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { .antMatchers("/formPoulain").permitAll() .antMatchers("/insertPoulain").permitAll() .antMatchers("/allHelpRequest").permitAll() .antMatchers("/formHelpRequest").permitAll() .antMatchers("/formHelpRequest").hasAnyAuthority("POULAIN") .antMatchers("/login").permitAll() .antMatchers("/register").permitAll() .antMatchers("/admin/**").hasAnyAuthority("ADMIN") .antMatchers("/member/**").hasAnyAuthority("POULAIN", "MENTOR") .and() .formLogin() .loginPage("/login") .permitAll() .loginPage("/login").permitAll() .usernameParameter("email") .successHandler(successHandler) .and() .logout() .permitAll(); .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) .logoutSuccessUrl("/").and() .exceptionHandling() .accessDeniedPage("/access-denied"); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("mentor").password("{noop}mentor").roles("MENTOR") .and().withUser("poulain").password("{noop}poulain").roles("POULAIN"); auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder()); } } No newline at end of file