master
tszyszkowski 2022-03-25 14:23:03 +01:00
parent 90c696bd54
commit 542dd89422
4 changed files with 11 additions and 6 deletions

View File

@ -26,14 +26,15 @@ public class ExtToken extends UsernamePasswordAuthenticationToken {
public boolean validate(String clientToken) { public boolean validate(String clientToken) {
String sha3 = DigestUtils.sha3_512Hex(clientToken + timestamp); String sha3 = DigestUtils.sha3_512Hex(clientToken + timestamp);
// logger.info("Token to compare: " + sha3);
TimeZone tz = TimeZone.getTimeZone("UTC"); TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
df.setTimeZone(tz); df.setTimeZone(tz);
try { try {
Date parsedTimestamp = df.parse(timestamp); Date parsedTimestamp = df.parse(timestamp);
Long timeDiff = parsedTimestamp.getTime() - Timestamp.from(Instant.now()).getTime(); Long timeDiff = parsedTimestamp.getTime() - Timestamp.from(Instant.now()).getTime();
System.out.println("Time diff: " + timeDiff); // logger.info("Time diff: " + timeDiff);
if (Math.abs(timeDiff) > 15000) { if (Math.abs(timeDiff) > 150000) {
return false; return false;
} }
} catch (ParseException e) { } catch (ParseException e) {

View File

@ -26,7 +26,7 @@ public class ExtTokenAuthenticationProvider extends AbstractUserDetailsAuthentic
protected UserDetails retrieveUser(String s, UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) throws AuthenticationException { protected UserDetails retrieveUser(String s, UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) throws AuthenticationException {
ExtToken token = (ExtToken) usernamePasswordAuthenticationToken; ExtToken token = (ExtToken) usernamePasswordAuthenticationToken;
if (token.validate(extToken)) { if (token.validate(extToken)) {
logger.info("Token validated"); // logger.info("Token validated");
return new ExtUser(); return new ExtUser();
} else { } else {
logger.info("Token not valid"); logger.info("Token not valid");

View File

@ -14,17 +14,19 @@ import java.io.IOException;
public class ExtTokenAuthenticationTokenFilter extends AbstractAuthenticationProcessingFilter { public class ExtTokenAuthenticationTokenFilter extends AbstractAuthenticationProcessingFilter {
private final static String TOKEN_HEADER = "Authorization"; private final static String TOKEN_HEADER = "Authorization";
private final static String TIMESTAMP_HEADER = "X-Timestamp"; private final static String TIMESTAMP_HEADER = "Timestamp";
private static final Logger logger = LoggerFactory.getLogger(ExtTokenAuthenticationTokenFilter.class); private static final Logger logger = LoggerFactory.getLogger(ExtTokenAuthenticationTokenFilter.class);
public ExtTokenAuthenticationTokenFilter() { public ExtTokenAuthenticationTokenFilter() {
super("/api/**"); super("/**");
} }
@Override @Override
public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException { public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException {
String authToken = httpServletRequest.getHeader(TOKEN_HEADER); String authToken = httpServletRequest.getHeader(TOKEN_HEADER);
String timestamp = httpServletRequest.getHeader(TIMESTAMP_HEADER); String timestamp = httpServletRequest.getHeader(TIMESTAMP_HEADER);
// logger.info("Token:" + authToken);
// logger.info("Timestamp:" + timestamp);
return getAuthenticationManager().authenticate(new ExtToken(authToken, timestamp)); return getAuthenticationManager().authenticate(new ExtToken(authToken, timestamp));
} }

View File

@ -4,4 +4,6 @@ server.port=9095
spring.kafka.producer.bootstrap-servers=localhost:9092 spring.kafka.producer.bootstrap-servers=localhost:9092
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.properties.max.request.size=20242880 spring.kafka.producer.properties.max.request.size=20242880
logging.level.org.springframework.web=INFO