Browse Source

first commit

tags/V2.2.0
chenjiandong 2 years ago
parent
commit
681cd1c172
5 changed files with 26 additions and 14 deletions
  1. +2
    -3
      tuoheng_oidc_server/src/main/java/com/tuoheng/config/SecurityConfig.java
  2. +3
    -1
      tuoheng_oidc_server/src/main/java/com/tuoheng/mapper/AuthoritiesMapper.java
  3. +2
    -2
      tuoheng_oidc_server/src/main/java/com/tuoheng/model/param/CreateUserDto.java
  4. +14
    -6
      tuoheng_oidc_server/src/main/java/com/tuoheng/service/impl/UserServiceImpl.java
  5. +5
    -2
      tuoheng_oidc_server/src/main/resources/mapper/AuthoritiesMapper.xml

+ 2
- 3
tuoheng_oidc_server/src/main/java/com/tuoheng/config/SecurityConfig.java View File

@@ -94,9 +94,8 @@ public class SecurityConfig {
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeHttpRequests((authorize) -> authorize
.antMatchers("/getHealth").hasRole("ADMIN")
.antMatchers("/login").permitAll()
.antMatchers("/static/**").permitAll()
.antMatchers("/login", "/getHealth", "/static/**").permitAll()
.antMatchers("/user/create").permitAll()
.anyRequest().authenticated()
)
// Form login handles the redirect to the login page from the

+ 3
- 1
tuoheng_oidc_server/src/main/java/com/tuoheng/mapper/AuthoritiesMapper.java View File

@@ -3,6 +3,8 @@ package com.tuoheng.mapper;
import com.tuoheng.model.po.AuthoritiesPo;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
* @author chenjiandong
* @description: TODO
@@ -11,6 +13,6 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface AuthoritiesMapper {

int insertAuthorities(AuthoritiesPo authoritiesPo);
int batchInsert(List<AuthoritiesPo> list);

}

+ 2
- 2
tuoheng_oidc_server/src/main/java/com/tuoheng/model/param/CreateUserDto.java View File

@@ -20,7 +20,7 @@ public class CreateUserDto {
@NotEmpty(message = "password can not be empty!")
private String password;

@NotNull(message = "roles can not be empty!")
private List<String> roles;
@NotNull(message = "authorities can not be null!")
private List<String> authorities;

}

+ 14
- 6
tuoheng_oidc_server/src/main/java/com/tuoheng/service/impl/UserServiceImpl.java View File

@@ -15,6 +15,11 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.expression.Lists;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* @author chenjiandong
@@ -36,13 +41,16 @@ public class UserServiceImpl implements UserSevice {
.setUsername(createUserDto.getUsername())
.setPassword("{bcrypt}" + new BCryptPasswordEncoder().encode(createUserDto.getPassword()));
userMapper.insertUser(userPo);
String roles = StringUtils.join(createUserDto.getRoles(),',');

AuthoritiesPo authoritiesPo = new AuthoritiesPo()
.setUserId(userPo.getId())
.setUsername(createUserDto.getUsername())
.setAuthority(roles);
authoritiesMapper.insertAuthorities(authoritiesPo);
List<AuthoritiesPo> authoritiesPos = new ArrayList<>();
for(String authority : createUserDto.getAuthorities()){
AuthoritiesPo authoritiesPo = new AuthoritiesPo()
.setUserId(userPo.getId())
.setUsername(createUserDto.getUsername())
.setAuthority(authority);
authoritiesPos.add(authoritiesPo);
}
authoritiesMapper.batchInsert(authoritiesPos);
return JsonResult.success(userPo.getId());
}


+ 5
- 2
tuoheng_oidc_server/src/main/resources/mapper/AuthoritiesMapper.xml View File

@@ -2,9 +2,12 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tuoheng.mapper.AuthoritiesMapper">

<insert id="insertAuthorities" parameterType="com.tuoheng.model.po.AuthoritiesPo">
<insert id="batchInsert" parameterType="java.util.List">
insert into authorities (user_id, username, authority)
values (#{userId}, #{username}, #{authority})
VALUES
<foreach collection ="list" item="it" separator =",">
(#{it.userId}, #{it.username}, #{it.authority})
</foreach >
</insert>

</mapper>

Loading…
Cancel
Save