所有的ticket都在serviceTicketRegistry这里放着,
从request取grant ticket,然后去serviceTicketRegistry里取就OK了。
直接贴代码片段吧。
public class GetSubSystemUrlAction extends AbstractJdbcUsernamePasswordAuthenticationHandler { @NotNull private TicketRegistry serviceTicketRegistry;
protected Logger logger = LoggerFactory.getLogger(getClass()); public void setServiceTicketRegistry(final TicketRegistry serviceTicketRegistry) { this.serviceTicketRegistry = serviceTicketRegistry; }
public final String get(final RequestContext context, final Credentials credentials) throws Exception {
if (credentials instanceof UsernamePasswordCredentials) { final UsernamePasswordCredentials cre = (UsernamePasswordCredentials) credentials;
// 当前用户名 String username = cre.getUsername(); if (username == null) { String ticketGrantingTicketId = WebUtils.getTicketGrantingTicketId(context); System.out.println("------tgt:-----------" + ticketGrantingTicketId); final TicketGrantingTicket ticketGrantingTicket; ticketGrantingTicket = (TicketGrantingTicket) this.serviceTicketRegistry.getTicket(ticketGrantingTicketId, TicketGrantingTicket.class); System.out.println("------tgt2:-----------" +ticketGrantingTicket); if(ticketGrantingTicket==null){ System.out.println("------to login:-----------"); return "error"; } final List<Authentication> chainedAuthenticationsList = ticketGrantingTicket.getChainedAuthentications(); final Authentication authentication = chainedAuthenticationsList.get(chainedAuthenticationsList.size() - 1); final Principal principal = authentication.getPrincipal(); username = principal.getId(); } context.getRequestScope().put("username", username);
我这个是登录流程里加的action,所以参数里有一个Credentials credentials,这个是用户输入的信息。
当刷新页面,或从其他地方跳回/login时,这个参数里面是没有值的,
username就是null,这时就需要使用request传来的ticketGrantingTicketId 来取了。
如果ticketGrantingTicket是null,说明它过期了,比如放置了好久没点页面,这时就跳到login重新登录就行了。
如果ticketGrantingTicket不为null,那么直接就能取到认证信息了。
我费了牛劲才发现的方法,共享~~
|