chenjiandong 2 лет назад
Родитель
Сommit
5640b7f469
7 измененных файлов: 96 добавлений и 45 удалений
  1. +9
    -2
      Spring_Authorization_Server/src/main/java/com/tuoheng/config/SecurityConfig.java
  2. +3
    -0
      Spring_Authorization_Server/src/main/java/com/tuoheng/mapper/UserMapper.java
  3. +19
    -0
      Spring_Authorization_Server/src/main/java/com/tuoheng/model/dto/UserBaseInfoDto.java
  4. +4
    -41
      Spring_Authorization_Server/src/main/java/com/tuoheng/service/OidcUserInfoService.java
  5. +51
    -0
      Spring_Authorization_Server/src/main/java/com/tuoheng/service/impl/OidcUserInfoServiceImpl.java
  6. +2
    -2
      Spring_Authorization_Server/src/main/java/com/tuoheng/service/impl/UserServiceImpl.java
  7. +8
    -0
      Spring_Authorization_Server/src/main/resources/mapper/UserMapper.xml

+ 9
- 2
Spring_Authorization_Server/src/main/java/com/tuoheng/config/SecurityConfig.java Просмотреть файл

@@ -1,6 +1,9 @@
package com.tuoheng.config;

import com.tuoheng.mapper.UserMapper;
import com.tuoheng.model.dto.UserBaseInfoDto;
import com.tuoheng.service.OidcUserInfoService;
import com.tuoheng.service.impl.OidcUserInfoServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
@@ -39,18 +42,22 @@ public class SecurityConfig {
@Autowired
private DataSource dataSource;

@Autowired
private UserMapper userMapper;

@Bean
@Order(1)
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer<HttpSecurity> authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer<>();

OidcUserInfoService oidcUserInfoService = new OidcUserInfoService();
OidcUserInfoServiceImpl oidcUserInfoService = new OidcUserInfoServiceImpl();
//自定义用户映射器
Function<OidcUserInfoAuthenticationContext, OidcUserInfo> userInfoMapper = (context) -> {
OidcUserInfoAuthenticationToken authentication = context.getAuthentication();
JwtAuthenticationToken principal = (JwtAuthenticationToken) authentication.getPrincipal();
return oidcUserInfoService.loadUser(principal.getName(), context.getAccessToken().getScopes());
UserBaseInfoDto userBaseInfoDto = userMapper.getUserBaseInfo(principal.getName());
return oidcUserInfoService.loadUser(principal.getName(), context.getAccessToken().getScopes(), userBaseInfoDto);
};
authorizationServerConfigurer.oidc((oidc) -> {
oidc.userInfoEndpoint((userInfo) -> userInfo.userInfoMapper(userInfoMapper));

+ 3
- 0
Spring_Authorization_Server/src/main/java/com/tuoheng/mapper/UserMapper.java Просмотреть файл

@@ -1,5 +1,6 @@
package com.tuoheng.mapper;

import com.tuoheng.model.dto.UserBaseInfoDto;
import com.tuoheng.model.po.UserPo;
import org.apache.ibatis.annotations.Mapper;

@@ -13,4 +14,6 @@ public interface UserMapper {

int insertUser(UserPo userPo);

UserBaseInfoDto getUserBaseInfo(String username);

}

+ 19
- 0
Spring_Authorization_Server/src/main/java/com/tuoheng/model/dto/UserBaseInfoDto.java Просмотреть файл

@@ -0,0 +1,19 @@
package com.tuoheng.model.dto;

import lombok.Data;

/**
* @author chenjiandong
* @description: TODO
* @date 2022/10/9 13:46
*/
@Data
public class UserBaseInfoDto {

private Integer userId;

private String userName;

private String authority;

}

+ 4
- 41
Spring_Authorization_Server/src/main/java/com/tuoheng/service/OidcUserInfoService.java Просмотреть файл

@@ -1,13 +1,7 @@
package com.tuoheng.service;

import com.nimbusds.jose.shaded.json.JSONObject;
import org.springframework.security.oauth2.core.oidc.OidcScopes;
import com.tuoheng.model.dto.UserBaseInfoDto;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Set;

/**
@@ -15,39 +9,8 @@ import java.util.Set;
* @description: TODO
* @date 2022/9/29 10:17
*/
public class OidcUserInfoService {
public interface OidcUserInfoService {

OidcUserInfo loadUser(String name, Set<String> scopes, UserBaseInfoDto userBaseInfoDto);

public OidcUserInfo loadUser(String name, Set<String> scopes) {
OidcUserInfo.Builder builder = OidcUserInfo.builder().subject(name);
if (!CollectionUtils.isEmpty(scopes)) {
if (scopes.contains(OidcScopes.PROFILE)) {
builder.name("First Last")
.givenName("First")
.familyName("Last")
.middleName("Middle")
.nickname("User")
.preferredUsername(name)
.profile("http://127.0.0.1:8080/" + name)
.picture("http://127.0.0.1:8080/" + name + ".jpg")
.website("http://127.0.0.1:8080/")
.gender("female")
.birthdate("2022-05-24")
.zoneinfo("China/Beijing")
.locale("zh-cn")
.updatedAt(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
}
if (scopes.contains(OidcScopes.EMAIL)) {
builder.email(name + "@163.com").emailVerified(true);
}
if (scopes.contains(OidcScopes.ADDRESS)) {
JSONObject address = new JSONObject();
address.put("address", Collections.singletonMap("formatted", "Champ de Mars\n5 Av. Anatole France\n75007 Paris\nFrance"));
builder.address(address.toJSONString());
}
if (scopes.contains(OidcScopes.PHONE)) {
builder.phoneNumber("13728903134").phoneNumberVerified("false");
}
}
return builder.build();
}
}

+ 51
- 0
Spring_Authorization_Server/src/main/java/com/tuoheng/service/impl/OidcUserInfoServiceImpl.java Просмотреть файл

@@ -0,0 +1,51 @@
package com.tuoheng.service.impl;

import com.nimbusds.jose.shaded.json.JSONObject;
import com.tuoheng.mapper.UserMapper;
import com.tuoheng.model.dto.UserBaseInfoDto;
import com.tuoheng.service.OidcUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.core.oidc.OidcScopes;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Collections;
import java.util.Set;

/**
* @author chenjiandong
* @description: TODO
* @date 2022/10/9 13:40
*/
@Service
public class OidcUserInfoServiceImpl implements OidcUserInfoService {

@Override
public OidcUserInfo loadUser(String name, Set<String> scopes, UserBaseInfoDto userBaseInfoDto) {
OidcUserInfo.Builder builder = OidcUserInfo.builder().subject(String.valueOf(Calendar.getInstance().getTimeInMillis()));
if (!CollectionUtils.isEmpty(scopes)) {
if (scopes.contains(OidcScopes.PROFILE)) {
builder.claim("userId", userBaseInfoDto.getUserId())
.claim("userName", userBaseInfoDto.getUserName())
.claim("authority", userBaseInfoDto.getAuthority());
}
/*if (scopes.contains(OidcScopes.EMAIL)) {
builder.email(name + "@163.com").emailVerified(true);
}
if (scopes.contains(OidcScopes.ADDRESS)) {
JSONObject address = new JSONObject();
address.put("address", Collections.singletonMap("formatted", "Champ de Mars\n5 Av. Anatole France\n75007 Paris\nFrance"));
builder.address(address.toJSONString());
}
if (scopes.contains(OidcScopes.PHONE)) {
builder.phoneNumber("13728903134").phoneNumberVerified("false");
}*/
}
return builder.build();
}

}

+ 2
- 2
Spring_Authorization_Server/src/main/java/com/tuoheng/service/impl/UserServiceImpl.java Просмотреть файл

@@ -1,6 +1,5 @@
package com.tuoheng.service.impl;

import com.sun.deploy.util.StringUtils;
import com.tuoheng.mapper.AuthoritiesMapper;
import com.tuoheng.mapper.UserMapper;
import com.tuoheng.model.param.CreateUserDto;
@@ -8,6 +7,7 @@ import com.tuoheng.model.po.AuthoritiesPo;
import com.tuoheng.model.po.UserPo;
import com.tuoheng.service.UserSevice;
import com.tuoheng.until.JsonResult;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
@@ -36,7 +36,7 @@ public class UserServiceImpl implements UserSevice {
.setUsername(createUserDto.getUsername())
.setPassword("{bcrypt}" + new BCryptPasswordEncoder().encode(createUserDto.getPassword()));
userMapper.insertUser(userPo);
String roles = StringUtils.join(createUserDto.getRoles(),",");
String roles = StringUtils.join(createUserDto.getRoles(),',');

AuthoritiesPo authoritiesPo = new AuthoritiesPo()
.setUserId(userPo.getId())

+ 8
- 0
Spring_Authorization_Server/src/main/resources/mapper/UserMapper.xml Просмотреть файл

@@ -7,4 +7,12 @@
values (#{username}, #{password})
</insert>


<select id="getUserBaseInfo" resultType="com.tuoheng.model.dto.UserBaseInfoDto">
select a.id as userId, a.username as userName, b.authority
from users a
inner join authorities b on a.id = b.user_id
where a.username = #{username}
</select>

</mapper>

Загрузка…
Отмена
Сохранить