diff --git a/pom.xml b/pom.xml
index fb26856..a57869d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -32,6 +32,11 @@
8.0.33
+ com.alibaba.fastjson2
+ fastjson2
+ 2.0.26
+
+
io.jsonwebtoken
jjwt
0.9.0
@@ -42,6 +47,11 @@
true
+ javax.xml.bind
+ jaxb-api
+ 2.3.1
+
+
org.springframework.boot
spring-boot-starter-web
diff --git a/src/main/java/com/iflytop/nuclear/config/SecurityConfig.java b/src/main/java/com/iflytop/nuclear/config/SecurityConfig.java
index ef3fb7a..8ff19c7 100644
--- a/src/main/java/com/iflytop/nuclear/config/SecurityConfig.java
+++ b/src/main/java/com/iflytop/nuclear/config/SecurityConfig.java
@@ -46,7 +46,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
http.cors().and().csrf().disable()
.authorizeRequests()
// 注册接口,需要ADMIN用户才能访问
- .antMatchers("/account/register").hasRole("ADMIN")
+ // .antMatchers("/account/register").hasRole("ADMIN")
// 其他都放行了
.anyRequest().permitAll()
.and()
diff --git a/src/main/java/com/iflytop/nuclear/controller/AccountController.java b/src/main/java/com/iflytop/nuclear/controller/AccountController.java
index ebb3260..3e1c940 100755
--- a/src/main/java/com/iflytop/nuclear/controller/AccountController.java
+++ b/src/main/java/com/iflytop/nuclear/controller/AccountController.java
@@ -17,6 +17,7 @@ import java.util.Map;
*/
@Slf4j
@RestController
+@CrossOrigin
@RequestMapping("/account")
public class AccountController {
@@ -43,7 +44,7 @@ public class AccountController {
* @return
*/
@PostMapping("/register")
- @PreAuthorize("hasRole('ADMIN')")
+ // @PreAuthorize("hasRole('ADMIN')")
public ResponseData registerAccount(@RequestBody Map registerUser) {
log.info("-----------------注册账户开始-----------------");
boolean register = accountService.register(registerUser.get("username"), registerUser.get("password"));
@@ -53,4 +54,5 @@ public class AccountController {
}
return ResponseData.fail("注册失败");
}
+
}
diff --git a/src/main/java/com/iflytop/nuclear/entity/JwtUser.java b/src/main/java/com/iflytop/nuclear/entity/JwtUser.java
index 83c942e..4c03596 100644
--- a/src/main/java/com/iflytop/nuclear/entity/JwtUser.java
+++ b/src/main/java/com/iflytop/nuclear/entity/JwtUser.java
@@ -24,10 +24,12 @@ public class JwtUser implements UserDetails {
// 写一个能直接使用user创建jwtUser的构造器
public JwtUser(Account user) {
- id = user.getId();
- username = user.getUsername();
- password = user.getPassword();
- authorities = Collections.singleton(new SimpleGrantedAuthority(user.getRole()));
+ if (user != null) {
+ id = user.getId();
+ username = user.getUsername();
+ password = user.getPassword();
+ authorities = Collections.singleton(new SimpleGrantedAuthority(user.getRole()));
+ }
}
@Override
diff --git a/src/main/java/com/iflytop/nuclear/exception/JWTAccessDeniedHandler.java b/src/main/java/com/iflytop/nuclear/exception/JWTAccessDeniedHandler.java
index a0a2723..bc5490f 100644
--- a/src/main/java/com/iflytop/nuclear/exception/JWTAccessDeniedHandler.java
+++ b/src/main/java/com/iflytop/nuclear/exception/JWTAccessDeniedHandler.java
@@ -1,6 +1,8 @@
package com.iflytop.nuclear.exception;
+import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.iflytop.nuclear.utils.ResponseData;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
@@ -20,6 +22,6 @@ public class JWTAccessDeniedHandler implements AccessDeniedHandler {
httpServletResponse.setContentType("application/json; charset=utf-8");
httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
String reason = "统一处理,原因:" + e.getMessage();
- httpServletResponse.getWriter().write(new ObjectMapper().writeValueAsString(reason));
+ httpServletResponse.getWriter().write(JSONObject.toJSONString(ResponseData.fail(reason)));
}
}
diff --git a/src/main/java/com/iflytop/nuclear/exception/JWTAuthenticationEntryPoint.java b/src/main/java/com/iflytop/nuclear/exception/JWTAuthenticationEntryPoint.java
index c1cb110..5654d1e 100644
--- a/src/main/java/com/iflytop/nuclear/exception/JWTAuthenticationEntryPoint.java
+++ b/src/main/java/com/iflytop/nuclear/exception/JWTAuthenticationEntryPoint.java
@@ -1,6 +1,8 @@
package com.iflytop.nuclear.exception;
+import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.iflytop.nuclear.utils.ResponseData;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
@@ -23,6 +25,6 @@ public class JWTAuthenticationEntryPoint implements AuthenticationEntryPoint {
response.setContentType("application/json; charset=utf-8");
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
String reason = "统一处理,原因:" + authException.getMessage();
- response.getWriter().write(new ObjectMapper().writeValueAsString(reason));
+ response.getWriter().write(JSONObject.toJSONString(ResponseData.fail(reason)));
}
}
diff --git a/src/main/java/com/iflytop/nuclear/filter/JWTAuthenticationFilter.java b/src/main/java/com/iflytop/nuclear/filter/JWTAuthenticationFilter.java
index f2cd636..6a7cfea 100644
--- a/src/main/java/com/iflytop/nuclear/filter/JWTAuthenticationFilter.java
+++ b/src/main/java/com/iflytop/nuclear/filter/JWTAuthenticationFilter.java
@@ -1,9 +1,11 @@
package com.iflytop.nuclear.filter;
+import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iflytop.nuclear.entity.JwtUser;
import com.iflytop.nuclear.model.Account;
import com.iflytop.nuclear.utils.JwtTokenUtils;
+import com.iflytop.nuclear.utils.ResponseData;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
@@ -68,6 +70,10 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte
// 但是这里创建的token只是单纯的token
// 按照jwt的规定,最后请求的时候应该是 `Bearer token`
response.setHeader("token", JwtTokenUtils.TOKEN_PREFIX + token);
+ JSONObject res = new JSONObject();
+ res.put("token", JwtTokenUtils.TOKEN_PREFIX + token);
+ res.put("username", jwtUser.getUsername());
+ response.getWriter().write(JSONObject.toJSONString(ResponseData.success(res)));
}
@Override
diff --git a/src/main/java/com/iflytop/nuclear/service/impl/AccountServiceImpl.java b/src/main/java/com/iflytop/nuclear/service/impl/AccountServiceImpl.java
index 529d394..71b800f 100755
--- a/src/main/java/com/iflytop/nuclear/service/impl/AccountServiceImpl.java
+++ b/src/main/java/com/iflytop/nuclear/service/impl/AccountServiceImpl.java
@@ -31,7 +31,7 @@ public class AccountServiceImpl extends ServiceImpl impl
Account account = Account.builder()
.username(username)
.password(bCryptPasswordEncoder.encode(password))
- .role("ROLE_USER")
+ .role("ROLE_ADMIN")
.build();
return this.save(account);
}