@@ -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,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); | |||
} |
@@ -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; | |||
} |
@@ -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()); | |||
} | |||
@@ -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> |