All GCC vendors and employee details for IT department management
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
6.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package com.gcc_manpower.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.authentication.AuthenticationManager;
  5. import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
  6. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  7. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  8. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  9. import org.springframework.security.crypto.password.PasswordEncoder;
  10. import org.springframework.security.web.SecurityFilterChain;
  11. import org.springframework.security.web.header.writers.XXssProtectionHeaderWriter;
  12. @Configuration
  13. @EnableWebSecurity
  14. public class SecurityConfig {
  15. @Bean
  16. public PasswordEncoder passwordEncoder() {
  17. return new PasswordEncoder() {
  18. private final BCryptPasswordEncoder bcrypt = new BCryptPasswordEncoder();
  19. @Override
  20. public String encode(CharSequence rawPassword) {
  21. return bcrypt.encode(rawPassword);
  22. }
  23. @Override
  24. public boolean matches(CharSequence rawPassword, String dbPassword) {
  25. if (dbPassword != null && dbPassword.startsWith("$2a$")) {
  26. return bcrypt.matches(rawPassword, dbPassword);
  27. }
  28. return rawPassword.toString().equals(dbPassword);
  29. }
  30. };
  31. }
  32. @Bean
  33. public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
  34. return config.getAuthenticationManager();
  35. }
  36. @Bean
  37. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  38. http
  39. // Configure headers for clickjacking and XSS protection
  40. .headers(headers -> headers
  41. .frameOptions(frame -> frame.sameOrigin())
  42. .xssProtection(
  43. xss -> xss.headerValue(XXssProtectionHeaderWriter.HeaderValue.ENABLED_MODE_BLOCK)))
  44. // Configure URL authorization
  45. .authorizeHttpRequests(auth -> auth
  46. // Allow static resources
  47. .requestMatchers(
  48. "/assets/**",
  49. "/css/**",
  50. "/js/**",
  51. "/images/**",
  52. "/favicon.ico",
  53. "/error")
  54. .permitAll()
  55. // Allow login pages and login API endpoints
  56. .requestMatchers(
  57. "/login",
  58. "/api/val-app-usr-login",
  59. "/gcctonermanagement/login",
  60. "/gcctonermanagement/api/login",
  61. "/gccservicepaymenttracker/login",
  62. "/gccservicepaymenttracker/api/login",
  63. "/gccservicedocuments/login",
  64. "/gccservicedocuments/api/login")
  65. .permitAll()
  66. // Allow logout endpoints
  67. .requestMatchers(
  68. "/usr-logout",
  69. "/gcctonermanagement/logout",
  70. "/gccservicepaymenttracker/logout",
  71. "/gccservicedocuments/logout")
  72. .permitAll()
  73. // Require authentication for all other requests
  74. .anyRequest().authenticated())
  75. // Configure CSRF protection (ignore CSRF on the login endpoints only)
  76. .csrf(csrf -> csrf
  77. .ignoringRequestMatchers(
  78. "/api/val-app-usr-login",
  79. "/gcctonermanagement/api/login",
  80. "/gccservicepaymenttracker/api/login",
  81. "/gccservicedocuments/api/login"))
  82. // Session management configuration with module-aware invalid session
  83. // redirection
  84. .sessionManagement(session -> session
  85. .invalidSessionStrategy((request, response) -> {
  86. // Create a fresh session so requestedSessionIdValid won't cause endless loop
  87. request.getSession(true);
  88. String requestURI = request.getRequestURI();
  89. if (requestURI.contains("/gccservicepaymenttracker")) {
  90. response.sendRedirect(request.getContextPath() + "/gccservicepaymenttracker/login");
  91. } else if (requestURI.contains("/gcctonermanagement")) {
  92. response.sendRedirect(request.getContextPath() + "/gcctonermanagement/login");
  93. } else if (requestURI.contains("/gccservicedocuments")) {
  94. response.sendRedirect(request.getContextPath() + "/gccservicedocuments/login");
  95. } else {
  96. response.sendRedirect(request.getContextPath() + "/login");
  97. }
  98. }))
  99. // Exception handling for unauthenticated requests with module-aware redirection
  100. .exceptionHandling(exceptions -> exceptions
  101. .authenticationEntryPoint((request, response, authException) -> {
  102. String requestURI = request.getRequestURI();
  103. if (requestURI.contains("/gccservicepaymenttracker")) {
  104. response.sendRedirect(request.getContextPath() + "/gccservicepaymenttracker/login");
  105. } else if (requestURI.contains("/gcctonermanagement")) {
  106. response.sendRedirect(request.getContextPath() + "/gcctonermanagement/login");
  107. } else if (requestURI.contains("/gccservicedocuments")) {
  108. response.sendRedirect(request.getContextPath() + "/gccservicedocuments/login");
  109. } else {
  110. response.sendRedirect(request.getContextPath() + "/login");
  111. }
  112. }));
  113. return http.build();
  114. }
  115. }