Search code examples
authenticationopenid

open id log in with google and spring mvc


I'm trying to developing a spring mvc project with open id and google log in. I'm using java configuration. And the xml configuration is

 <openid-login user-service-ref="openIdUserService" >
  <attribute-exchange >
<openid-attribute name="email" type="http://schema.openid.net/contact/email"    required="true" />
  <openid-attribute name="firstName" type="http://axschema.org/namePerson/first" required="true" />
  <openid-attribute name="lastName" type="http://axschema.org/namePerson/last" required="true" />
</attribute-exchange>

but couldn't figure out what is the corresponding code in java configuration.

Any suggestion and some code example for that.

I'm using spring security too.

Here is the provider:

public class OpenIdUserDetailsService implements UserDetailsService, AuthenticationUserDetailsService {

    @Autowired
    private CustomerRepository userRepository;
    private static final List DEFAULT_AUTHORITIES = AuthorityUtils.createAuthorityList("ROLE_USER");

    @Override
    public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException {
        Customer user = userRepository.findByOpenIdIdentifier(id);

        if (user == null) {
            throw new UsernameNotFoundException(id);
        }
        OpenIdUser openIdUser = new OpenIdUser(user.getOpenIdIdentifier(), DEFAULT_AUTHORITIES);
        openIdUser.setName(user.getFirstname());

        return openIdUser;
    }

    @Override
    public UserDetails loadUserDetails(OpenIDAuthenticationToken token) {
        String id = token.getIdentityUrl();

        Customer user = userRepository.findByOpenIdIdentifier(id);

        if (user != null) {
            OpenIdUser openIdUser = new OpenIdUser(user.getOpenIdIdentifier(), DEFAULT_AUTHORITIES);
            openIdUser.setName(user.getFirstname());

            return openIdUser;
        }

        String firstName = null;
        String lastName = null;
        String fullName = null;

        List attributes = token.getAttributes();

        for (OpenIDAttribute attribute : attributes) {
            String name = attribute.getName();

            if (name.equals("firstname")) {
                firstName = attribute.getValues().get(0);
            } else if (name.equals("lastname")) {
                lastName = attribute.getValues().get(0);
            } else if (name.equals("fullname")) {
                fullName = attribute.getValues().get(0);
            }
        }

        if (fullName == null) {
            StringBuilder fullNameBldr = new StringBuilder();

            if (firstName != null) {
                fullNameBldr.append(firstName);
            }
            if (lastName != null) {
                fullNameBldr.append(" ").append(lastName);
            }

            fullName = fullNameBldr.toString();
        }

        OpenIdUser openIdUser = new OpenIdUser(id, DEFAULT_AUTHORITIES);
        openIdUser.setName(fullName);
        openIdUser.setNewUser(true);

        Customer u = new Customer();
        u.setOpenIdIdentifier(openIdUser.getUsername());
        u.setFirstname(openIdUser.getName());

        userRepository.save(u);

        return openIdUser;
    }
}

Thanks for all help.


Solution

  • OpenIDLoginConfigurer builder will build OpenIDAuthenticationFilter and register filter in spring security filter chain.

    OpenIDLoginConfigurer takes attributeExchange parameters to build OpenIDAuthenticationFilter.These are the callback parameters after authenticate with openid provider.

    To authenticate with open id through spring security,requested url will be "/j_spring_openid_security_check" OpenIDAuthenticationFilter will process this request,by hitting to openid provider for authentication.Once authentication is done, openid user will be mapped with local user.

    Here is simple example application that works openId login with spring security.