inject userinfo into context for use in JSPs

addresses #99 (for real this time)
pull/210/head
Justin Richer 2012-08-23 18:20:35 -04:00
parent b5ce8d5e8b
commit 37d6d63772
3 changed files with 53 additions and 1 deletions

View File

@ -0,0 +1,48 @@
/**
*
*/
package org.mitre.openid.connect.web;
import java.security.Principal;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mitre.openid.connect.model.UserInfo;
import org.mitre.openid.connect.repository.UserInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/**
* @author jricher
*
*/
public class UserInfoInterceptor extends HandlerInterceptorAdapter {
@Autowired
private UserInfoRepository userInfoRepository;
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// get our principal
Principal p = request.getUserPrincipal();
if (p != null && p.getName() != null) {
// try to look up a user based on it
UserInfo user = userInfoRepository.getByUserId(p.getName());
// if we have one, inject it so views can use it
if (user != null) {
modelAndView.addObject("userInfo", user);
}
}
}
}

View File

@ -21,6 +21,10 @@
<!-- Enables the Spring MVC @Controller programming model -->
<tx:annotation-driven transaction-manager="transactionManager" />
<mvc:annotation-driven />
<mvc:interceptors>
<!-- Inject the UserInfo into the current context -->
<bean id="userInfoInterceptor" class="org.mitre.openid.connect.web.UserInfoInterceptor" />
</mvc:interceptors>
<mvc:default-servlet-handler />
<!-- Bean to hold configuration propreties -->

View File

@ -21,7 +21,7 @@
<security:authentication property="authorities" var="roles"/>
<security:authorize access="hasRole('ROLE_USER')">
Logged in as <a href="#"><%= request.getUserPrincipal().getName() %></a>
Logged in as <a href="#">${ userInfo.preferredUsername }</a>
</security:authorize>
</p>
</div><!--/.nav-collapse -->