Project

General

Profile

1
package eu.dnetlib.organizations;
2

    
3
import java.io.IOException;
4

    
5
import javax.servlet.ServletException;
6
import javax.servlet.http.HttpServletRequest;
7
import javax.servlet.http.HttpServletResponse;
8

    
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11
import org.springframework.security.access.AccessDeniedException;
12
import org.springframework.security.core.Authentication;
13
import org.springframework.security.core.context.SecurityContextHolder;
14
import org.springframework.security.web.access.AccessDeniedHandler;
15
import org.springframework.stereotype.Component;
16

    
17
@Component
18
public class MyAccessDeniedHandler implements AccessDeniedHandler {
19

    
20
	private static Logger logger = LoggerFactory.getLogger(MyAccessDeniedHandler.class);
21

    
22
	@Override
23
	public void handle(final HttpServletRequest req, final HttpServletResponse res, final AccessDeniedException e)
24
			throws IOException, ServletException {
25

    
26
		final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
27

    
28
		if (auth != null) {
29
			logger.warn(String.format("User '%s' attempted to access the protected URL: %s", auth.getName(), req.getRequestURI()));
30
		}
31

    
32
		res.sendRedirect(req.getContextPath() + "/authorizationRequest");
33
	}
34

    
35
}
(2-2/3)