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
import eu.dnetlib.organizations.controller.UserInfo;
18

    
19
@Component
20
public class MyAccessDeniedHandler implements AccessDeniedHandler {
21

    
22
	private static Logger logger = LoggerFactory.getLogger(MyAccessDeniedHandler.class);
23

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

    
28
		final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
29

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

    
34
		if (UserInfo.isNotAuthorized(auth)) {
35
			res.sendRedirect(req.getContextPath() + "/authorizationRequest");
36
		} else {
37
			res.sendRedirect(req.getContextPath() + "/alreadyRegistered");
38
		}
39
	}
40

    
41
}
(2-2/3)