Bladeren bron

登陆模块重构

chenweida 7 jaren geleden
bovenliggende
commit
811df7d370

+ 26 - 12
web-gateway/src/main/java/com/yihu/jw/config/security/roleService/ClientService.java

@ -1,24 +1,38 @@
package com.yihu.jw.config.security.roleService;
import com.yihu.jw.feign.base.user.EmployFeign;
import org.springframework.beans.factory.annotation.Autowired;
import com.yihu.base.security.rbas.ClientServiceProvider;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Service;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Component;
import java.util.HashSet;
import java.util.Set;
/**
 * Created by chenweida on 2017/11/29.
 * 获取用户信息
 * Created by chenweida on 2017/12/1.
 */
@Service
public class ClientService implements ClientDetailsService {
    @Autowired
    private EmployFeign employeeFeign;
@Component("baseClientDetailsService")
public class ClientService implements ClientServiceProvider {
    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        return null;
//        SaasDO saasDO = saasDao.findByAppId(clientId);
//        if (saasDO == null) {
//            throw new ClientRegistrationException("用户没有注册");
//        }
        BaseClientDetails baseClientDetails = new BaseClientDetails();
        baseClientDetails.setClientId("cwd");
        baseClientDetails.setClientSecret("cwd");
        Set<String> strings = new HashSet<>();
        strings.add("password");
        strings.add("custom_password");
        strings.add("authorization_code");
        strings.add("refresh_token");
        baseClientDetails.setAuthorizedGrantTypes(strings);
        return baseClientDetails;
    }
}

+ 18 - 12
web-gateway/src/main/java/com/yihu/jw/config/security/roleService/UserRbasService.java

@ -1,9 +1,9 @@
package com.yihu.jw.config.security.roleService;
import com.yihu.base.security.rbas.RbasService;
import com.yihu.base.security.rbas.IRbasService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.AntPathMatcher;
import javax.servlet.http.HttpServletRequest;
@ -11,31 +11,37 @@ import java.util.HashSet;
import java.util.Set;
/**
 * Created by chenweida on 2017/11/30.
 * Created by chenweida on 2017/12/5.
 * 判断用户是否有权限访问该路径
 */
@Component("rbasService")
public class UserRbasService implements RbasService {
@Service("rbasService")
public class RbasService implements IRbasService {
    private AntPathMatcher antPathMatcher = new AntPathMatcher();
    @Override
    public Boolean hasPerssion(HttpServletRequest request, Authentication authentication) {
        Object principal = authentication.getPrincipal();
        boolean hasPerssion = false;
        if (principal instanceof UserDetails) {
            //用户名字
            String userName = ((UserDetails) principal).getUsername();
            //获取用户名字
            String username = ((UserDetails) principal).getUsername();
            //根据用户名字去数据库查找权限
            Set<String> urls = new HashSet<>();
            for (String url : urls) {
                if (antPathMatcher.match(url, request.getRequestURI())) {
            //获取用户全部权限
            Set<String> uris = new HashSet<>();
            for (String uri : uris) {
                if (antPathMatcher.match(uri, request.getRequestURI())) {
                    hasPerssion = true;
                    break;
                }
            }
        }
        return hasPerssion;
    }
    public Boolean hello() {
        return true;
    }
}

+ 41 - 8
web-gateway/src/main/java/com/yihu/jw/config/security/roleService/UserService.java

@ -1,25 +1,58 @@
package com.yihu.jw.config.security.roleService;
import com.yihu.jw.feign.base.user.EmployFeign;
import com.yihu.jw.restmodel.common.Envelop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
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;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
 * Created by chenweida on 2017/11/29.
 * 获取用户信息
 * 处理用户校验
 */
@Service
@Component
public class UserService implements UserDetailsService {
    @Autowired
    private EmployFeign employeeFeign;
    private PasswordEncoder passwordEncoder;
    /**
     * 我们只需要把用户返回给spring-security 密码框架自己帮我们校验
     *
     * @param userName
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        Envelop user= employeeFeign.getEmployeeByAccount(userName);
        return null;
        if ("admin".equals(userName)) {
            System.out.printf("password:" + passwordEncoder.encode("123456"));
            return new User("admin",
                    passwordEncoder.encode("123456"),
                    true,
                    true,
                    true,
                    true
                    , AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_USER") //权限
            );
        } else if ((!StringUtils.isEmpty(userName))&&userName.length() == 11) {
            System.out.printf("password:" + passwordEncoder.encode("123456"));
            return new User("admin",
                    passwordEncoder.encode("123456"),
                    true,
                    true,
                    true,
                    true
                    , AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_USER") //权限
            );
        } else {
            throw new UsernameNotFoundException("用户不存在");
        }
    }
}