Browse Source

🎉 oauth申请token可支持自定义表

test
smallchill 6 years ago
parent
commit
a8d9301e85
  1. 45
      blade-auth/src/main/java/org/springblade/auth/enums/BladeUserEnum.java
  2. 2
      blade-auth/src/main/java/org/springblade/auth/service/BladeUserDetails.java
  3. 18
      blade-auth/src/main/java/org/springblade/auth/service/BladeUserDetailsServiceImpl.java
  4. 2
      blade-auth/src/main/java/org/springblade/auth/utils/TokenUtil.java

45
blade-auth/src/main/java/org/springblade/auth/enums/BladeUserEnum.java

@ -0,0 +1,45 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.auth.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 用户类型枚举
*
* @author Chill
*/
@Getter
@AllArgsConstructor
public enum BladeUserEnum {
/**
* web
*/
WEB("web", 1),
/**
* app
*/
APP("app", 2),
;
final String name;
final int category;
}

2
blade-auth/src/main/java/org/springblade/auth/service/BladeUserDetails.java

@ -59,7 +59,7 @@ public class BladeUserDetails extends User {
*/
private String avatar;
BladeUserDetails(Long userId, String tenantId, String name, String roleId, String roleName, String avatar, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
public BladeUserDetails(Long userId, String tenantId, String name, String roleId, String roleName, String avatar, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
this.userId = userId;
this.tenantId = tenantId;

18
blade-auth/src/main/java/org/springblade/auth/service/BladeUserDetailsServiceImpl.java

@ -19,6 +19,7 @@ package org.springblade.auth.service;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.auth.constant.AuthConstant;
import org.springblade.auth.enums.BladeUserEnum;
import org.springblade.auth.utils.TokenUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
@ -49,8 +50,23 @@ public class BladeUserDetailsServiceImpl implements UserDetailsService {
@SneakyThrows
public UserDetails loadUserByUsername(String username) {
HttpServletRequest request = WebUtil.getRequest();
// 获取租户
String tenantId = Func.toStr(request.getHeader(TokenUtil.TENANT_HEADER_KEY), TokenUtil.DEFAULT_TENANT_ID);
R<UserInfo> result = userClient.userInfo(tenantId, username);
// 获取用户类型
String userType = Func.toStr(request.getHeader(TokenUtil.USER_TYPE_HEADER_KEY), TokenUtil.DEFAULT_USER_TYPE);
// 远程调用返回数据
R<UserInfo> result;
// 根据不同用户类型调用对应的接口返回数据,用户可自行拓展
if (userType.equals(BladeUserEnum.WEB.getName())) {
result = userClient.userInfo(tenantId, username);
} else if (userType.equals(BladeUserEnum.APP.getName())) {
result = userClient.userInfo(tenantId, username);
} else {
result = userClient.userInfo(tenantId, username);
}
// 判断返回信息
if (result.isSuccess()) {
User user = result.getData().getUser();
if (user == null) {

2
blade-auth/src/main/java/org/springblade/auth/utils/TokenUtil.java

@ -49,6 +49,8 @@ public class TokenUtil {
public final static String TENANT_HEADER_KEY = "Tenant-Id";
public final static String DEFAULT_TENANT_ID = "000000";
public final static String USER_TYPE_HEADER_KEY = "User-Type";
public final static String DEFAULT_USER_TYPE = "web";
public final static String USER_NOT_FOUND = "用户名或密码错误";
public final static String HEADER_KEY = "Authorization";
public final static String HEADER_PREFIX = "Basic ";

Loading…
Cancel
Save