Java tutorial
/******************************************************************************* * Copyright 2014 Juan Diego Navarre Gonzalez * * 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 net.navasoft.madcoin.backend.services.security; import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import java.util.Collection; import java.util.Properties; import net.navasoft.madcoin.backend.HardCodedConstants; import net.navasoft.madcoin.backend.services.controller.exception.AllowedExceptionMessage; import net.navasoft.madcoin.backend.services.exception.BadConfigException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.simple.ParameterizedRowMapper; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.stereotype.Component; /** * net.navasoft.madcoin.backend.services.security Class class UsernameMapper. * Description: * * @author Juan Diego Navarre Gonzalez - (${authorMail}) * @version 1.0 * @since 18/08/2014 04:36:22 PM */ @Component("providernameMapper") public class ProvidernameMapper implements ParameterizedRowMapper<User>, HardCodedConstants { /** * initializer. * * @since 18/08/2014, 04:36:22 PM */ private static Properties initializer = new Properties(); static { try { initializer.load(AllowedExceptionMessage.class.getResourceAsStream(providerProperties)); } catch (IOException e) { throw new BadConfigException("Query is not defined."); } } /** * authority mapper. * * @since 18/08/2014, 04:36:22 PM */ @Autowired @Qualifier("provider_authorityMapper") private RowMapper<SimpleGrantedAuthority> authorityMapper; /** * dao. * * @since 18/08/2014, 04:36:22 PM */ @Autowired @Qualifier("providerLoginAccess") private NamedParameterJdbcTemplate dao; /** * mapping. * * @since 18/08/2014, 04:36:22 PM */ @Autowired @Qualifier("providerMappedParameter") private MapSqlParameterSource mapping; /** * Map row. * * @param rs * the rs * @param arg1 * the arg1 * @return the user * @throws SQLException * the SQL exception * @since 18/08/2014, 04:36:22 PM */ @Override public User mapRow(ResultSet rs, int arg1) throws SQLException { String username = rs.getString(USER_COLUMN); return new User(username, rs.getString(PASSWORD_COLUMN), rs.getBoolean(ENABLED_COLUMN), true, true, true, getAuthorities(username)); } /** * Gets the authorities. * * @param username * the username * @return the authorities * @see * @since 18/08/2014, 04:36:22 PM */ private Collection<? extends GrantedAuthority> getAuthorities(String username) { String key = AccessProperties.PROVIDER_AUTHORITIES_QUERY.getConfigKey(); if (initializer.containsKey(key)) { mapping.addValue("username", username, Types.VARCHAR); return dao.query(initializer.getProperty(key), mapping, authorityMapper); } else { throw new BadConfigException("Query is not defined."); } } }