new auth, hot entries

This commit is contained in:
2021-10-05 14:39:49 +02:00
parent e9e9a22719
commit 4a2b8b08bb
13 changed files with 279 additions and 107 deletions
@@ -0,0 +1,72 @@
/**
*
*/
package de.bstly.board.security;
import java.util.Collection;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
/**
* The Class LocalUserDetails.
*/
public class LocalUserDetails extends User {
/**
* default serialVersionUID
*/
private static final long serialVersionUID = 1L;
private double gravity;
private long karma;
/**
* Instantiates a new local user details.
*
* @param username the username
* @param password the password
* @param authorities the authorities
*/
public LocalUserDetails(String username, String password,
Collection<? extends GrantedAuthority> authorities) {
super(username, password, authorities);
}
/**
* Gets the gravity.
*
* @return the gravity
*/
public double getGravity() {
return gravity;
}
/**
* Sets the gravity.
*
* @param gravity the new gravity
*/
public void setGravity(double gravity) {
this.gravity = gravity;
}
/**
* Gets the karma.
*
* @return the karma
*/
public long getKarma() {
return karma;
}
/**
* Sets the karma.
*
* @param karma the new karma
*/
public void setKarma(long karma) {
this.karma = karma;
}
}
@@ -14,9 +14,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
@@ -26,7 +25,6 @@ import com.google.common.collect.Lists;
import de.bstly.board.businesslogic.UserManager;
import de.bstly.board.model.LocalUser;
/**
* The Class OAuth2AuthenticationSuccessHandler.
*/
@@ -34,34 +32,33 @@ import de.bstly.board.model.LocalUser;
public class OAuth2AuthenticationSuccessHandler
extends SavedRequestAwareAuthenticationSuccessHandler {
@Autowired
private UserManager localUserManager;
private RememberMeServices rememberMeServices;
/*
* @see org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler#onAuthenticationSuccess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.core.Authentication)
*/
/*
* @see org.springframework.security.web.authentication.
* SavedRequestAwareAuthenticationSuccessHandler#onAuthenticationSuccess(javax.
* servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
* org.springframework.security.core.Authentication)
*/
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
LocalUser localUser = localUserManager.getByAuth(authentication);
UserDetails userDetails = localUserManager.loadUserByUsername(localUser.getUsername());
List<GrantedAuthority> authorities = Lists.newArrayList();
authorities.addAll(authentication.getAuthorities());
if (localUser.getRoles() != null) {
for (String role : localUser.getRoles()) {
authorities.add(new SimpleGrantedAuthority(role));
}
}
User user = new User(localUser.getUsername(), "", authorities);
authorities.addAll(userDetails.getAuthorities());
UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(
user, null, authorities);
userDetails, null, authorities);
SecurityContextHolder.getContext().setAuthentication(newAuthentication);