com.ram.topup.business.service.authentication.impl.AuthenticationServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.ram.topup.business.service.authentication.impl.AuthenticationServiceImpl.java

Source

/**
 * Copyright (c) 2013-2014 RAM Corporation
 *
 * 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 com.ram.topup.business.service.authentication.impl;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import com.ram.topup.business.service.authentication.AuthenticationService;
import com.ram.topup.business.service.authentication.userdetails.ExtendedUserDetailsImpl;
import com.ram.topup.shared.entities.UserProfile;
import com.ram.topup.shared.utils.exception.TopupException;

// TODO: Auto-generated Javadoc
/**
 * The Class AuthenticationServiceImpl.
 */
@Service("authenticationService")
public class AuthenticationServiceImpl implements AuthenticationService {

    /** The user service. */
    @Autowired
    private UserDetailsService userService;

    /** The authentication manager. */
    @Resource(name = "authenticationManager")
    private AuthenticationManager authenticationManager; // specific for Spring Security

    /* (non-Javadoc)
     * @see com.ram.topup.business.service.authentication.AuthenticationService#login(java.lang.String, java.lang.String)
     */
    @Override
    public UserProfile login(String username, String password) {
        try {
            Authentication authenticate = authenticationManager
                    .authenticate(new UsernamePasswordAuthenticationToken(username, password));
            if (authenticate.isAuthenticated()) {
                SecurityContextHolder.getContext().setAuthentication(authenticate);
                return ((ExtendedUserDetailsImpl) authenticate.getPrincipal()).getUser().toUserProfile();
            } else {
                throw new TopupException("Authentication Failed with username=" + username);
            }
        } catch (AuthenticationException e) {
            throw new TopupException("Authentication Error Occured", e);
        }

    }

    /* (non-Javadoc)
     * @see com.ram.topup.business.service.authentication.AuthenticationService#logout()
     */
    @Override
    public void logout() {
        SecurityContextHolder.clearContext();
    }

}