Skip to content

Commit

Permalink
Add AuthUtil to get current user and add customer auth handler
Browse files Browse the repository at this point in the history
  • Loading branch information
teahan105 committed Feb 28, 2024
1 parent 315beb9 commit e335c8d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.HeaderWriterLogoutHandler;
import org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
Expand All @@ -23,6 +24,8 @@ public class SpringSecurity {

private UserDetailsService userDetailsService;

private AuthenticationSuccessHandler customAuthenticationSuccessHandler;

@Bean
public static PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
Expand All @@ -36,18 +39,21 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.requestMatchers("/crx/**", "/css/**", "/img/**", "/js/**", "/vendor/**").permitAll()
.requestMatchers("/register/**").permitAll()
.requestMatchers("/forgot-password/**").permitAll()
.requestMatchers("/home").hasRole("ADMIN")
.requestMatchers("/home", "/").hasRole("ADMIN")
.requestMatchers("/selenium/**").hasRole("ADMIN")
).formLogin(
form -> form
.loginPage("/login")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/home", true)
// .defaultSuccessUrl("/home", true)
.successHandler(customAuthenticationSuccessHandler)
.permitAll()

).logout(
logout -> logout
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(ClearSiteDataHeaderWriter.Directive.ALL)))
.logoutSuccessUrl("/login")
.permitAll()
);
return http.build();
Expand All @@ -59,4 +65,5 @@ public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.hanstack.linkedintool.enums.ToolbarEnum;
import com.hanstack.linkedintool.model.User;
import com.hanstack.linkedintool.service.UserService;
import com.hanstack.linkedintool.util.AuthUtil;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
Expand All @@ -22,6 +23,11 @@ public class AuthController {

private UserService userService;

@GetMapping("/")
public String defaultHome() {
return "redirect:/home";
}

@GetMapping("/home")
public String home(Model model, HttpSession httpSession) {
FilterDTO filterDTO = FilterDTO.builder()
Expand All @@ -40,6 +46,9 @@ public String home(Model model, HttpSession httpSession) {

@GetMapping("/login")
public String loginForm() {
if (AuthUtil.isLogin()) {
return "redirect:/home";
}
return "layout/auth/login";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.hanstack.linkedintool.security;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.WebAttributes;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@NoArgsConstructor
@Component
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
handle(request, response, authentication);
clearAuthenticationAttributes(request);
}

protected void handle(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException {
final String targetUrl = determineTargetUrl(authentication);

if (response.isCommitted()) {
log.debug("Response has already been committed. Unable to redirect to " + targetUrl);
return;
}

redirectStrategy.sendRedirect(request, response, targetUrl);
}

protected String determineTargetUrl(final Authentication authentication) {

Map<String, String> roleTargetUrlMap = new HashMap<>();
roleTargetUrlMap.put("ROLE_USER", "/home");
roleTargetUrlMap.put("ROLE_ADMIN", "/home");

final Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
for (final GrantedAuthority grantedAuthority : authorities) {

String authorityName = grantedAuthority.getAuthority();
if (roleTargetUrlMap.containsKey(authorityName)) {
return roleTargetUrlMap.get(authorityName);
}
}

throw new IllegalStateException();
}

/**
* Removes temporary authentication-related data which may have been stored in the session
* during the authentication process.
*/
protected final void clearAuthenticationAttributes(final HttpServletRequest request) {
final HttpSession session = request.getSession(false);

if (session == null) {
return;
}

session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/hanstack/linkedintool/util/AuthUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.hanstack.linkedintool.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Objects;

public class AuthUtil {
public static boolean isLogin() {
try {
return Objects.nonNull(getCurrentUser());
} catch (Exception e) {
return false;
}

}

public static UserDetails getCurrentUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

if (authentication.getPrincipal() instanceof String || Objects.isNull(authentication.getPrincipal())) {
return null;
}

UserDetails userDetails = (UserDetails) authentication.getPrincipal();

if (StringUtils.isEmpty(userDetails.getUsername())) {
return null;
}

return userDetails;
}
}

0 comments on commit e335c8d

Please sign in to comment.