Changeset 8bcd64c for src/main/java/edu/gjoko/schedlr/config
- Timestamp:
- 04/19/23 21:19:08 (19 months ago)
- Branches:
- master
- Children:
- 950fa0d
- Parents:
- 9050790
- Location:
- src/main/java/edu/gjoko/schedlr/config
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/edu/gjoko/schedlr/config/AppConfig.java
r9050790 r8bcd64c 5 5 import org.springframework.security.config.core.GrantedAuthorityDefaults; 6 6 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 7 import org.springframework.security.web.authentication.AuthenticationFailureHandler; 7 8 import org.springframework.security.web.authentication.AuthenticationSuccessHandler; 8 9 … … 15 16 16 17 @Bean 17 public AuthenticationSuccessHandler myApplicationAuthenticationSuccessHandler() {18 public AuthenticationSuccessHandler appAuthenticationSuccessHandler() { 18 19 return new AppAuthenticationSuccessHandler(); 20 } 21 22 @Bean 23 public AuthenticationFailureHandler appAuthenticationFailureHandler() { 24 return new AppAuthenticationFailureHandler(); 19 25 } 20 26 -
src/main/java/edu/gjoko/schedlr/config/AppFilter.java
r9050790 r8bcd64c 1 1 package edu.gjoko.schedlr.config; 2 2 3 import edu.gjoko.schedlr.services.PostgresUserDetailsService; 4 import lombok.RequiredArgsConstructor; 3 5 import org.springframework.security.core.GrantedAuthority; 4 6 import org.springframework.security.core.context.SecurityContextImpl; 5 7 import org.springframework.security.core.userdetails.UserDetails; 6 8 import org.springframework.util.StringUtils; 9 import org.springframework.web.bind.annotation.RequestBody; 7 10 import org.springframework.web.filter.GenericFilterBean; 8 11 … … 12 15 import javax.servlet.ServletResponse; 13 16 import javax.servlet.http.HttpServletRequest; 17 import javax.servlet.http.HttpServletResponse; 14 18 import javax.servlet.http.HttpSession; 15 19 import java.io.IOException; … … 18 22 import java.util.Map; 19 23 24 @RequiredArgsConstructor 20 25 public class AppFilter extends GenericFilterBean { 26 27 private final PostgresUserDetailsService userDetailsService; 21 28 @Override 22 29 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 23 30 HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; 24 31 HttpSession session = httpServletRequest.getSession(false); 32 HttpServletResponse response = (HttpServletResponse) servletResponse; 25 33 26 34 if(httpServletRequest.getRequestURI().endsWith(".js") 27 || httpServletRequest.getRequestURI().endsWith(".css") 28 || httpServletRequest.getRequestURI().startsWith("/api")) { 35 || httpServletRequest.getRequestURI().endsWith(".css")) { 29 36 filterChain.doFilter(servletRequest, servletResponse); 30 37 return; 31 38 } 39 40 if(httpServletRequest.getRequestURI().startsWith("/api")) { 41 session = ((HttpServletRequest) servletRequest).getSession(true); 42 SecurityContextImpl sci = (SecurityContextImpl) session.getAttribute("SPRING_SECURITY_CONTEXT"); 43 44 if(sci != null && session.getAttribute("stakeholderId") == null) { 45 UserDetails userDetails = (UserDetails) sci.getAuthentication().getPrincipal(); 46 Long stakeholderId = userDetailsService.loadStakeholderId(userDetails.getUsername()); 47 session.setAttribute("stakeholderId", stakeholderId); 48 } 49 filterChain.doFilter(servletRequest, servletResponse); 50 return; 51 } 52 32 53 if(session != null) { 33 Map<String, String> roleTargetUrlMap = new HashMap<>();34 roleTargetUrlMap.put("ADMIN", "/admin");35 roleTargetUrlMap.put("CUSTOMER", "/homepage");36 roleTargetUrlMap.put("BUSINESS_OWNER", "/business_homepage");37 54 SecurityContextImpl sci = (SecurityContextImpl) session.getAttribute("SPRING_SECURITY_CONTEXT"); 38 55 if(sci != null) { 39 56 UserDetails userDetails = (UserDetails) sci.getAuthentication().getPrincipal(); 57 Long stakeholderId = userDetailsService.loadStakeholderId(userDetails.getUsername()); 58 session.setAttribute("stakeholderId", stakeholderId); 59 40 60 final Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities(); 41 61 for (final GrantedAuthority grantedAuthority : authorities) { … … 47 67 break; 48 68 case "CUSTOMER": 69 page = "/homepage"; 70 break; 49 71 case "BUSINESS_OWNER": 50 page = "/ homepage";72 page = "/business_admin"; 51 73 break; 52 74 default: -
src/main/java/edu/gjoko/schedlr/config/AppSecurityConfig.java
r9050790 r8bcd64c 2 2 3 3 import edu.gjoko.schedlr.services.PostgresUserDetailsService; 4 import lombok.AllArgsConstructor; 4 5 import org.springframework.context.annotation.Bean; 5 6 import org.springframework.context.annotation.Configuration; … … 11 12 import org.springframework.security.config.core.GrantedAuthorityDefaults; 12 13 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 14 import org.springframework.security.web.access.AccessDeniedHandler; 15 import org.springframework.security.web.authentication.AuthenticationFailureHandler; 13 16 import org.springframework.security.web.authentication.AuthenticationSuccessHandler; 14 17 import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; … … 16 19 @Configuration 17 20 @EnableWebSecurity 21 @AllArgsConstructor 18 22 public class AppSecurityConfig extends WebSecurityConfigurerAdapter { 19 23 … … 24 28 private final AuthenticationSuccessHandler authenticationSuccessHandler; 25 29 26 public AppSecurityConfig(PostgresUserDetailsService userDetailsService, BCryptPasswordEncoder passwordEncoder, 27 AuthenticationSuccessHandler authenticationSuccessHandler) { 28 this.userDetailsService = userDetailsService; 29 this.passwordEncoder = passwordEncoder; 30 this.authenticationSuccessHandler = authenticationSuccessHandler; 31 } 30 private final AuthenticationFailureHandler authenticationFailureHandler; 32 31 33 32 @Bean … … 48 47 .authenticationEntryPoint(new AppAuthenticationEntryPoint()) 49 48 .and() 50 .addFilter Before(new AppFilter(), BasicAuthenticationFilter.class)49 .addFilterAfter(new AppFilter(userDetailsService), BasicAuthenticationFilter.class) 51 50 .formLogin() 52 51 .loginPage("/login") 53 52 .loginProcessingUrl("/login") 54 53 .successHandler(authenticationSuccessHandler) 54 .failureHandler(authenticationFailureHandler) 55 55 .defaultSuccessUrl("/homepage") 56 56 .and() -
src/main/java/edu/gjoko/schedlr/config/MvcConfig.java
r9050790 r8bcd64c 2 2 3 3 import org.springframework.context.annotation.Configuration; 4 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;5 4 import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 6 5 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; … … 16 15 registry.addViewController("/homepage").setViewName("homepage"); 17 16 registry.addViewController("/admin").setViewName("admin"); 18 } 19 20 @Override 21 public void addResourceHandlers(ResourceHandlerRegistry registry) { 22 registry.addResourceHandler("/resources/**") 23 .addResourceLocations("/resources/"); 17 registry.addViewController("/business_admin").setViewName("business_admin"); 24 18 } 25 19 }
Note:
See TracChangeset
for help on using the changeset viewer.