All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.khs.sherpa.sping.SpringAuthentication Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
package com.khs.sherpa.sping;

/*
 * Copyright 2012 the original author or authors.
 *
 * 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.
 */

import java.util.ArrayList;
import java.util.List;

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.GrantedAuthority;

import com.khs.sherpa.exception.SherpaPermissionExcpetion;
import com.khs.sherpa.json.service.UserService;

public class SpringAuthentication implements UserService {

	@Autowired
	private AuthenticationManager authenticationManager;
	
	public String[] authenticate(String username, String password) {
		UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
		
		Authentication authentication = authenticationManager.authenticate(token);
		if(authentication.isAuthenticated() == false) {
			throw new SherpaPermissionExcpetion("username and/or password is incorrect");
		}
		
		List roles = new ArrayList();
		for(GrantedAuthority auth: authentication.getAuthorities()) {
			roles.add(auth.getAuthority());
		}
		
		return roles.toArray(new String[roles.size()]);
		
	}


	public AuthenticationManager getAuthenticationManager() {
		return authenticationManager;
	}


	public void setAuthenticationManager(AuthenticationManager authenticationManager) {
		this.authenticationManager = authenticationManager;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy