2021-12-07 10:23:36 +01:00
|
|
|
package eu.dnetlib.uoaauthorizationlibrary.stateless.security;
|
2020-09-03 21:40:48 +02:00
|
|
|
|
2021-12-07 10:23:36 +01:00
|
|
|
import eu.dnetlib.uoaauthorizationlibrary.security.OpenAIREAuthentication;
|
2020-09-03 21:40:48 +02:00
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import javax.servlet.*;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
@Component
|
|
|
|
public class AuthorizationFilter implements Filter {
|
|
|
|
|
2021-11-16 14:37:06 +01:00
|
|
|
private final AuthorizationProvider authorizationProvider;
|
2020-09-03 21:40:48 +02:00
|
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
|
|
|
|
@Autowired
|
2021-11-16 14:37:06 +01:00
|
|
|
AuthorizationFilter(AuthorizationProvider authorizationProvider) {
|
2020-09-03 21:40:48 +02:00
|
|
|
this.authorizationProvider = authorizationProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-16 14:37:06 +01:00
|
|
|
public void init(FilterConfig filterConfig) throws ServletException {}
|
2020-09-03 21:40:48 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
|
2021-11-16 14:37:06 +01:00
|
|
|
OpenAIREAuthentication auth = authorizationProvider.getAuthentication((HttpServletRequest) req);
|
2021-11-16 15:43:53 +01:00
|
|
|
if(auth.isAuthenticated()) {
|
2021-11-16 14:37:06 +01:00
|
|
|
SecurityContextHolder.getContext().setAuthentication(auth);
|
2020-09-03 21:40:48 +02:00
|
|
|
}
|
|
|
|
filterChain.doFilter(req, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void destroy() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|