1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.security.authn.interceptors;
17
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20
21 import org.apache.log4j.Logger;
22 import org.kuali.mobility.security.user.api.User;
23 import org.kuali.mobility.security.authn.util.AuthenticationConstants;
24 import org.kuali.mobility.security.authn.util.AuthenticationMapper;
25 import org.springframework.web.servlet.HandlerInterceptor;
26 import org.springframework.web.servlet.ModelAndView;
27
28
29
30
31
32
33
34
35
36
37 public class AuthenticationInterceptor implements HandlerInterceptor {
38
39 private static final Logger LOG = Logger.getLogger( AuthenticationInterceptor.class);
40
41 private AuthenticationMapper authenticationMapper;
42
43 @Override
44 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
45 User user = (User)request.getSession(true).getAttribute( AuthenticationConstants.KME_USER_KEY );
46
47 boolean passThrough = true;
48 if( getAuthenticationMapper().requiresAuthentication( request.getServletPath() ) )
49 {
50 if( user == null )
51 {
52 LOG.info( "User object not found in session. This should not happen." );
53 doLogin( request, response );
54 passThrough=false;
55 }
56 else if( user.isPublicUser() )
57 {
58 user.setRequestURL(request.getServletPath());
59 doLogin( request, response );
60 passThrough=false;
61 }
62 }
63 return passThrough;
64 }
65
66 @Override
67 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}
68
69 @Override
70 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}
71
72 private void doLogin(HttpServletRequest request, HttpServletResponse response) throws Exception
73 {
74 if( getAuthenticationMapper().getLoginURL().startsWith( "http:" ) )
75 {
76 response.sendRedirect( getAuthenticationMapper().getLoginURL() );
77 }
78 else
79 {
80 response.sendRedirect( request.getContextPath() + getAuthenticationMapper().getLoginURL() );
81 }
82 }
83
84 public AuthenticationMapper getAuthenticationMapper() {
85 return authenticationMapper;
86 }
87
88 public void setAuthenticationMapper(AuthenticationMapper authenticationMapper) {
89 this.authenticationMapper = authenticationMapper;
90 }
91
92 }