Java tutorial
/** * * APDPlat - Application Product Development Platform * Copyright (c) 2013, ??, yang-shangchuan@qq.com * * 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 3 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, see <http://www.gnu.org/licenses/>. * */ package org.apdplat.module.security.service; import org.apdplat.module.security.model.User; import org.apdplat.module.security.service.filter.IPAccessControler; import org.apdplat.platform.criteria.Criteria; import org.apdplat.platform.criteria.Operator; import org.apdplat.platform.criteria.PropertyCriteria; import org.apdplat.platform.criteria.PropertyEditor; import org.apdplat.platform.filter.OpenEntityManagerInViewFilter; import org.apdplat.platform.log.APDPlatLogger; import org.apdplat.platform.result.Page; import org.apdplat.platform.service.ServiceFacade; import org.apdplat.platform.util.FileUtils; import java.util.Collection; import java.util.HashMap; import java.util.Map; import javax.annotation.Resource; import org.apache.commons.lang.StringUtils; import org.apdplat.module.system.service.PropertyHolder; import org.apdplat.platform.log.APDPlatLoggerFactory; import org.springframework.dao.DataAccessException; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.web.util.TextEscapeUtils; import org.springframework.stereotype.Service; /** * ?? * ?org.springframework.security.core.userdetails.UserDetailsService * UserDetails loadUserByUsername(String username) throws UsernameNotFoundException; * @author ?? */ @Service public class UserDetailsServiceImpl implements UserDetailsService { private static final APDPlatLogger LOG = APDPlatLoggerFactory.getAPDPlatLogger(UserDetailsServiceImpl.class); @Resource(name = "serviceFacade") private ServiceFacade serviceFacade; public static String SPRING_SECURITY_LAST_USERNAME = null; private static Map<String, String> messages = new HashMap<>(); private String message; private static final IPAccessControler ipAccessControler = new IPAccessControler(); /** * JSP?? * @param username ?? * @return */ public synchronized static String getMessage(String username) { String result = messages.get(TextEscapeUtils.escapeEntities(username)); LOG.debug("??? " + username + " :" + result); messages.remove(TextEscapeUtils.escapeEntities(username)); return result; } /** * ? * @param username ?? * @return ? * @throws UsernameNotFoundException ?? */ @Override public synchronized UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { //spring security????? SPRING_SECURITY_LAST_USERNAME = username; //try catchfinally?? try { if (ipAccessControler.deny(OpenEntityManagerInViewFilter.request)) { message = "IP?"; LOG.info(message); throw new UsernameNotFoundException(message); } return load(username); } catch (UsernameNotFoundException e) { throw e; } finally { LOG.debug("??? " + username + " " + message); messages.put(TextEscapeUtils.escapeEntities(username), message); } } private UserDetails load(String username) throws UsernameNotFoundException { if (FileUtils.existsFile("/WEB-INF/licence") && PropertyHolder.getBooleanProperty("security")) { Collection<String> reqs = FileUtils.getTextFileContent("/WEB-INF/licence"); message = "?"; if (reqs != null && reqs.size() == 1) { message += ":" + reqs.iterator().next().toString(); } LOG.info(message); throw new UsernameNotFoundException(message); } if (StringUtils.isBlank(username)) { message = "??"; LOG.info(message); throw new UsernameNotFoundException(message); } /* ? */ PropertyCriteria propertyCriteria = new PropertyCriteria(Criteria.or); propertyCriteria.addPropertyEditor(new PropertyEditor("username", Operator.eq, "String", username)); //PropertyEditor sub1=new PropertyEditor(Criteria.or); //sub1.addSubPropertyEditor(new PropertyEditor("id", Operator.eq, 1)); //sub1.addSubPropertyEditor(new PropertyEditor("id", Operator.eq, 2)); //PropertyEditor sub=new PropertyEditor(Criteria.and); //sub.addSubPropertyEditor(new PropertyEditor("id", Operator.ne, 6)); //sub.addSubPropertyEditor(new PropertyEditor("id", Operator.ne, 7)); //sub.addSubPropertyEditor(new PropertyEditor("id", Operator.ne, 8)); //sub.addSubPropertyEditor(sub1); //propertyCriteria.addPropertyEditor(sub); Page<User> page = serviceFacade.query(User.class, null, propertyCriteria); if (page.getTotalRecords() != 1) { message = "??"; LOG.info(message + ": " + username); throw new UsernameNotFoundException(message); } User user = page.getModels().get(0); message = user.loginValidate(); if (message != null) { LOG.info(message); throw new UsernameNotFoundException(message); } //????? message = "??"; return user; } }