drew %!s(int64=6) %!d(string=hai) anos
pai
achega
a34e046e6b

+ 2 - 4
src/main/java/com/izouma/awesomeAdmin/domain/Menu.java

@@ -3,9 +3,7 @@ package com.izouma.awesomeAdmin.domain;
 import lombok.Data;
 import lombok.Data;
 import org.hibernate.annotations.Where;
 import org.hibernate.annotations.Where;
 
 
-import javax.persistence.Entity;
-import javax.persistence.JoinColumn;
-import javax.persistence.OneToMany;
+import javax.persistence.*;
 import java.io.Serializable;
 import java.io.Serializable;
 import java.util.List;
 import java.util.List;
 
 
@@ -30,6 +28,6 @@ public class Menu extends BaseEntity implements Serializable {
     private Boolean active;
     private Boolean active;
 
 
     @OneToMany
     @OneToMany
-    @JoinColumn(name = "parent", insertable = false, updatable = false)
+    @JoinColumn(name = "parent", insertable = false, updatable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
     List<Menu> children;
     List<Menu> children;
 }
 }

+ 5 - 3
src/main/java/com/izouma/awesomeAdmin/domain/User.java

@@ -18,7 +18,9 @@ import javax.validation.constraints.Pattern;
 import javax.validation.constraints.Size;
 import javax.validation.constraints.Size;
 import java.io.Serializable;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
 import java.util.List;
+import java.util.Set;
 
 
 @Data
 @Data
 @Entity
 @Entity
@@ -49,11 +51,11 @@ public class User extends BaseEntity implements Serializable {
     @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH})
     @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH})
     @JoinTable(
     @JoinTable(
             name = "user_authority",
             name = "user_authority",
-            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
-            inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "name")})
+            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))},
+            inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "name", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))})
     @BatchSize(size = 20)
     @BatchSize(size = 20)
     @ExcelIgnore
     @ExcelIgnore
-    private List<Authority> authorities = new ArrayList<>();
+    private Set<Authority> authorities = new HashSet<>();
 
 
     private String openId;
     private String openId;
 
 

+ 2 - 1
src/main/java/com/izouma/awesomeAdmin/security/JwtUserFactory.java

@@ -5,6 +5,7 @@ import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 
 
 import java.util.List;
 import java.util.List;
+import java.util.Set;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 public final class JwtUserFactory {
 public final class JwtUserFactory {
@@ -16,7 +17,7 @@ public final class JwtUserFactory {
         return new JwtUser(user, mapToGrantedAuthorities(user.getAuthorities()));
         return new JwtUser(user, mapToGrantedAuthorities(user.getAuthorities()));
     }
     }
 
 
-    private static List<GrantedAuthority> mapToGrantedAuthorities(List<Authority> authorities) {
+    private static List<GrantedAuthority> mapToGrantedAuthorities(Set<Authority> authorities) {
         if (authorities != null) {
         if (authorities != null) {
             return authorities.stream()
             return authorities.stream()
                               .map(authority -> new SimpleGrantedAuthority(authority.getName()))
                               .map(authority -> new SimpleGrantedAuthority(authority.getName()))

+ 29 - 10
src/main/java/com/izouma/awesomeAdmin/service/UserService.java

@@ -17,21 +17,21 @@ import me.chanjar.weixin.mp.api.WxMpService;
 import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
 import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
 import me.chanjar.weixin.mp.bean.result.WxMpUser;
 import me.chanjar.weixin.mp.bean.result.WxMpUser;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.RandomStringUtils;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
+import javax.annotation.PostConstruct;
 import java.text.SimpleDateFormat;
 import java.text.SimpleDateFormat;
-import java.util.Collections;
-import java.util.Date;
-import java.util.UUID;
+import java.util.*;
 
 
 @Service
 @Service
 @Slf4j
 @Slf4j
 @AllArgsConstructor
 @AllArgsConstructor
 public class UserService {
 public class UserService {
-    private UserRepo       userRepo;
-    private WxMaService    wxMaService;
-    private WxMpService    wxMpService;
-    private SmsService     smsService;
+    private UserRepo userRepo;
+    private WxMaService wxMaService;
+    private WxMpService wxMpService;
+    private SmsService smsService;
     private StorageService storageService;
     private StorageService storageService;
 
 
     public User loginByPhone(String phone) {
     public User loginByPhone(String phone) {
@@ -54,7 +54,7 @@ public class UserService {
                        .openId(wxMpUser.getOpenId())
                        .openId(wxMpUser.getOpenId())
                        .language(wxMpUser.getLanguage())
                        .language(wxMpUser.getLanguage())
                        .enabled(true)
                        .enabled(true)
-                       .authorities(Collections.singletonList(Authority.builder().name("ROLE_USER").build()))
+                       .authorities(Collections.singleton(Authority.builder().name("ROLE_USER").build()))
                        .build();
                        .build();
             userRepo.save(user);
             userRepo.save(user);
         }
         }
@@ -76,7 +76,7 @@ public class UserService {
                            .openId(openId)
                            .openId(openId)
                            .avatar(Constants.DEFAULT_AVATAR)
                            .avatar(Constants.DEFAULT_AVATAR)
                            .enabled(true)
                            .enabled(true)
-                           .authorities(Collections.singletonList(Authority.builder().name("ROLE_USER").build()))
+                           .authorities(Collections.singleton(Authority.builder().name("ROLE_USER").build()))
                            .build();
                            .build();
             userInfo = userRepo.save(userInfo);
             userInfo = userRepo.save(userInfo);
             return userInfo;
             return userInfo;
@@ -120,7 +120,7 @@ public class UserService {
                        .province(wxUserInfo.getProvince())
                        .province(wxUserInfo.getProvince())
                        .city(wxUserInfo.getCity())
                        .city(wxUserInfo.getCity())
                        .enabled(true)
                        .enabled(true)
-                       .authorities(Collections.singletonList(Authority.builder().name("ROLE_USER").build()))
+                       .authorities(Collections.singleton(Authority.builder().name("ROLE_USER").build()))
                        .build();
                        .build();
             user = userRepo.save(user);
             user = userRepo.save(user);
 
 
@@ -136,4 +136,23 @@ public class UserService {
 
 
         return user;
         return user;
     }
     }
+
+    @PostConstruct
+    public void init() {
+        User root = userRepo.findByUsername("root");
+        if (root == null) {
+            Set<Authority> authorities = new HashSet<>();
+            authorities.add(new Authority(Authority.NAMES.ROLE_ADMIN.name()));
+            authorities.add(new Authority(Authority.NAMES.ROLE_USER.name()));
+            root = User.builder()
+                       .nickname("管理员")
+                       .username("root")
+                       .password(new BCryptPasswordEncoder().encode("123456"))
+                       .avatar(Constants.DEFAULT_AVATAR)
+                       .enabled(true)
+                       .authorities(authorities)
+                       .build();
+            userRepo.save(root);
+        }
+    }
 }
 }

+ 8 - 0
src/main/java/com/izouma/awesomeAdmin/web/AuthorityController.java

@@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.bind.annotation.*;
 
 
+import javax.annotation.PostConstruct;
 import java.util.List;
 import java.util.List;
 
 
 @RestController
 @RestController
@@ -25,4 +26,11 @@ public class AuthorityController extends BaseController {
     public Authority save(Authority authority) {
     public Authority save(Authority authority) {
         return authorityRepo.save(authority);
         return authorityRepo.save(authority);
     }
     }
+
+    @PostConstruct
+    public void init() {
+        authorityRepo.save(new Authority(Authority.NAMES.ROLE_ADMIN.name()));
+        authorityRepo.save(new Authority(Authority.NAMES.ROLE_USER.name()));
+        authorityRepo.save(new Authority(Authority.NAMES.ROLE_DEV.name()));
+    }
 }
 }

+ 2 - 3
src/main/java/com/izouma/awesomeAdmin/web/UserController.java

@@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.*;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.List;
 
 
 @AllArgsConstructor
 @AllArgsConstructor
@@ -31,14 +32,12 @@ public class UserController extends BaseController {
     @PostMapping("/register")
     @PostMapping("/register")
     public User register(@RequestParam String username,
     public User register(@RequestParam String username,
                          @RequestParam String password) {
                          @RequestParam String password) {
-        List<Authority> authorities = new ArrayList<>();
-        authorities.add(new Authority(Authority.NAMES.ROLE_USER.name()));
         User user = User.builder()
         User user = User.builder()
                         .username(username)
                         .username(username)
                         .nickname(username)
                         .nickname(username)
                         .password(new BCryptPasswordEncoder().encode(password))
                         .password(new BCryptPasswordEncoder().encode(password))
                         .enabled(true)
                         .enabled(true)
-                        .authorities(authorities)
+                        .authorities(Collections.singleton(new Authority(Authority.NAMES.ROLE_USER.name())))
                         .build();
                         .build();
         return userRepo.save(user);
         return userRepo.save(user);
     }
     }