info.gewton.openid.servlet.OpenIDLoginServlet.java Source code

Java tutorial

Introduction

Here is the source code for info.gewton.openid.servlet.OpenIDLoginServlet.java

Source

/*
 * Copyright 2011 Gewton Jhames <gewtonj@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package info.gewton.openid.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.openid.OpenIDAttribute;
import org.springframework.security.openid.OpenIDAuthenticationToken;

/**
 * Servlet que dever ser disparado aps autenticao realizada com sucesso 
 * com o provedor OpenID. <br />
 * O propsito deste servlet  adicionar o atributo <tt>namePerson</tt> na sesso.
 * Este atributo representa o nome do usurio que efetuou login junto ao <em>provider</em>
 * via <em>Attribute Exchange</em> (AX). Caso contrrio, a nica informao que
 * a aplicao ter do usurio  seu <em>identifier</em>.
 * 
 * @author gewton
 */
public class OpenIDLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        SecurityContext sc = (SecurityContext) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        OpenIDAuthenticationToken auth = (OpenIDAuthenticationToken) sc.getAuthentication();
        List<OpenIDAttribute> attributes = auth.getAttributes();
        for (OpenIDAttribute a : attributes) {
            if (a.getName().equals("namePerson")) {
                request.getSession().setAttribute("namePerson", a.getValues().get(0));
                break;
            }
        }
        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }
}