-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AuthUtil to get current user and add customer auth handler
- Loading branch information
Showing
4 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/com/hanstack/linkedintool/security/CustomAuthenticationSuccessHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
src/main/java/com/hanstack/linkedintool/util/AuthUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |