mirror of
https://gitee.com/dromara/MaxKey.git
synced 2026-05-14 20:50:14 +08:00
OnlineTicket
This commit is contained in:
@@ -149,6 +149,9 @@ public class RealmAuthenticationProvider extends AbstractAuthenticationProvider
|
||||
String onlineTickitId = WebConstants.ONLINE_TICKET_PREFIX + "-" + java.util.UUID.randomUUID().toString().toLowerCase();
|
||||
_logger.debug("set online Tickit Cookie " + onlineTickitId + " on domain "+ this.applicationConfig.getBaseDomainName());
|
||||
|
||||
OnlineTicket onlineTicket = new OnlineTicket(onlineTickitId);
|
||||
|
||||
|
||||
WebContext.setCookie(WebContext.getResponse(),
|
||||
this.applicationConfig.getBaseDomainName(),
|
||||
WebConstants.ONLINE_TICKET_NAME,
|
||||
@@ -157,7 +160,7 @@ public class RealmAuthenticationProvider extends AbstractAuthenticationProvider
|
||||
|
||||
SigninPrincipal signinPrincipal = new SigninPrincipal(userInfo);
|
||||
//set OnlineTicket
|
||||
signinPrincipal.setOnlineTicket(onlineTickitId);
|
||||
signinPrincipal.setOnlineTicket(onlineTicket);
|
||||
ArrayList<GrantedAuthority> grantedAuthoritys = authenticationRealm.grantAuthority(userInfo);
|
||||
//set default roles
|
||||
grantedAuthoritys.add(new SimpleGrantedAuthority("ROLE_USER"));
|
||||
@@ -182,8 +185,10 @@ public class RealmAuthenticationProvider extends AbstractAuthenticationProvider
|
||||
authenticationToken.setDetails(
|
||||
new WebAuthenticationDetails(WebContext.getRequest()));
|
||||
|
||||
OnlineTicket onlineTicket = new OnlineTicket(onlineTickitId,authenticationToken);
|
||||
onlineTicket.setAuthentication(authenticationToken);
|
||||
|
||||
this.onlineTicketServices.store(onlineTickitId, onlineTicket);
|
||||
|
||||
/*
|
||||
* put userInfo to current session context
|
||||
*/
|
||||
|
||||
@@ -20,6 +20,7 @@ package org.maxkey.authn;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.maxkey.authn.online.OnlineTicket;
|
||||
import org.maxkey.domain.UserInfo;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
@@ -31,7 +32,7 @@ public class SigninPrincipal implements UserDetails {
|
||||
|
||||
UserDetails userDetails;
|
||||
|
||||
String onlineTicket;
|
||||
OnlineTicket onlineTicket;
|
||||
ArrayList<GrantedAuthority> grantedAuthority;
|
||||
boolean authenticated;
|
||||
boolean roleAdministrators;
|
||||
@@ -95,11 +96,11 @@ public class SigninPrincipal implements UserDetails {
|
||||
this.grantedAuthority = grantedAuthority;
|
||||
}
|
||||
|
||||
public String getOnlineTicket() {
|
||||
public OnlineTicket getOnlineTicket() {
|
||||
return onlineTicket;
|
||||
}
|
||||
|
||||
public void setOnlineTicket(String onlineTicket) {
|
||||
public void setOnlineTicket(OnlineTicket onlineTicket) {
|
||||
this.onlineTicket = onlineTicket;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,15 +18,19 @@
|
||||
package org.maxkey.authn.online;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalTime;
|
||||
|
||||
import org.ehcache.UserManagedCache;
|
||||
import org.ehcache.config.builders.ExpiryPolicyBuilder;
|
||||
import org.ehcache.config.builders.UserManagedCacheBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
public class InMemoryOnlineTicketServices implements OnlineTicketServices{
|
||||
|
||||
protected final static UserManagedCache<String, OnlineTicket> onlineTicketStore =
|
||||
private static final Logger _logger = LoggerFactory.getLogger(InMemoryOnlineTicketServices.class);
|
||||
|
||||
protected static UserManagedCache<String, OnlineTicket> onlineTicketStore =
|
||||
UserManagedCacheBuilder.newUserManagedCacheBuilder(String.class, OnlineTicket.class)
|
||||
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMinutes(30)))
|
||||
.build(true);
|
||||
@@ -56,8 +60,37 @@ public class InMemoryOnlineTicketServices implements OnlineTicketServices{
|
||||
|
||||
@Override
|
||||
public void setValiditySeconds(int validitySeconds) {
|
||||
// TODO Auto-generated method stub
|
||||
onlineTicketStore =
|
||||
UserManagedCacheBuilder.
|
||||
newUserManagedCacheBuilder(String.class, OnlineTicket.class)
|
||||
.withExpiry(
|
||||
ExpiryPolicyBuilder.timeToLiveExpiration(
|
||||
Duration.ofMinutes(validitySeconds/60))
|
||||
)
|
||||
.build(true);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh(String ticketId,LocalTime refreshTime) {
|
||||
OnlineTicket onlineTicket = get(ticketId);
|
||||
onlineTicket.setTicketTime(refreshTime);
|
||||
store(ticketId , onlineTicket);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh(String ticketId) {
|
||||
OnlineTicket onlineTicket = get(ticketId);
|
||||
|
||||
LocalTime currentTime = LocalTime.now();
|
||||
Duration duration = Duration.between(currentTime, onlineTicket.getTicketTime());
|
||||
|
||||
_logger.trace("OnlineTicket duration " + duration.getSeconds());
|
||||
|
||||
if(duration.getSeconds() > OnlineTicket.MAX_EXPIRY_DURATION) {
|
||||
onlineTicket.setTicketTime(currentTime);
|
||||
refresh(ticketId,currentTime);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.maxkey.authn.online;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalTime;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.maxkey.domain.apps.Apps;
|
||||
@@ -11,10 +12,15 @@ public class OnlineTicket implements Serializable{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 9008067569150338296L;
|
||||
|
||||
public static final int MAX_EXPIRY_DURATION = 60 * 10; //default 10 minutes.
|
||||
|
||||
private static final long serialVersionUID = 9008067569150338296L;
|
||||
|
||||
public String ticketId;
|
||||
|
||||
public LocalTime ticketTime;
|
||||
|
||||
public Authentication authentication;
|
||||
|
||||
private HashMap<String , Apps> authorizedApps = new HashMap<String , Apps>();
|
||||
@@ -23,12 +29,14 @@ public class OnlineTicket implements Serializable{
|
||||
public OnlineTicket(String ticketId) {
|
||||
super();
|
||||
this.ticketId = ticketId;
|
||||
this.ticketTime = LocalTime.now();
|
||||
}
|
||||
|
||||
public OnlineTicket(String ticketId,Authentication authentication) {
|
||||
super();
|
||||
this.ticketId = ticketId;
|
||||
this.authentication = authentication;
|
||||
this.ticketTime = LocalTime.now();
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +52,14 @@ public class OnlineTicket implements Serializable{
|
||||
}
|
||||
|
||||
|
||||
public LocalTime getTicketTime() {
|
||||
return ticketTime;
|
||||
}
|
||||
|
||||
public void setTicketTime(LocalTime ticketTime) {
|
||||
this.ticketTime = ticketTime;
|
||||
}
|
||||
|
||||
public Authentication getAuthentication() {
|
||||
return authentication;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
package org.maxkey.authn.online;
|
||||
|
||||
import java.time.LocalTime;
|
||||
|
||||
public interface OnlineTicketServices {
|
||||
|
||||
@@ -25,6 +26,10 @@ public interface OnlineTicketServices {
|
||||
public OnlineTicket remove(String ticket);
|
||||
|
||||
public OnlineTicket get(String ticketId);
|
||||
|
||||
public void refresh(String ticketId ,LocalTime refreshTime);
|
||||
|
||||
public void refresh(String ticketId);
|
||||
|
||||
public void setValiditySeconds(int validitySeconds);
|
||||
}
|
||||
|
||||
@@ -17,12 +17,17 @@
|
||||
|
||||
package org.maxkey.authn.online;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalTime;
|
||||
|
||||
import org.maxkey.persistence.redis.RedisConnection;
|
||||
import org.maxkey.persistence.redis.RedisConnectionFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
public class RedisOnlineTicketServices implements OnlineTicketServices {
|
||||
|
||||
private static final Logger _logger = LoggerFactory.getLogger(RedisOnlineTicketServices.class);
|
||||
|
||||
protected int serviceTicketValiditySeconds = 60 * 30; //default 30 minutes.
|
||||
|
||||
@@ -78,5 +83,27 @@ public class RedisOnlineTicketServices implements OnlineTicketServices {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh(String ticketId,LocalTime refreshTime) {
|
||||
OnlineTicket onlineTicket = get(ticketId);
|
||||
onlineTicket.setTicketTime(refreshTime);
|
||||
store(ticketId , onlineTicket);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh(String ticketId) {
|
||||
OnlineTicket onlineTicket = get(ticketId);
|
||||
|
||||
LocalTime currentTime = LocalTime.now();
|
||||
Duration duration = Duration.between(currentTime, onlineTicket.getTicketTime());
|
||||
|
||||
_logger.trace("OnlineTicket duration " + duration.getSeconds());
|
||||
|
||||
if(duration.getSeconds() > OnlineTicket.MAX_EXPIRY_DURATION) {
|
||||
onlineTicket.setTicketTime(currentTime);
|
||||
refresh(ticketId,currentTime);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user