Java tutorial
/* * Copyright (C) 2015 Nuuptech * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package com.redhat.rhtracking.web.domain; import com.redhat.rhtracking.core.services.UserService; import com.redhat.rhtracking.events.EventStatus; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; /** * * @author marco-g8 */ public class SecureUser implements UserDetailsService { @Autowired private UserService service; private static final Logger logger = Logger.getLogger(SecureUser.class); @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Map<String, Object> request = new HashMap<>(); request.put("username", username); Map<String, Object> response = service.findByUsername(request); if (response.get("status") != EventStatus.SUCCESS) throw new UsernameNotFoundException("User not found"); Collection<GrantedAuthority> roles = new ArrayList<>(); for (String s : (List<String>) response.get("roles")) { roles.add(new SimpleGrantedAuthority(s)); } return new User((String) response.get("username"), (String) response.get("password"), (boolean) response.get("enabled"), (boolean) response.get("accountNonExpired"), (boolean) response.get("credentialsNonExpired"), (boolean) response.get("accountNonLocked"), roles); } }