-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3d9350b
Showing
169 changed files
with
8,132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Default ignored files | ||
/shelf/ | ||
/workspace.xml | ||
*iml | ||
*target/ | ||
.idea | ||
*.DS_Store | ||
*application-private.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>bcmall</artifactId> | ||
<groupId>cn.xjjdog.bcmall</groupId> | ||
<version>0.9.9</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>bc-admin</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>cn.xjjdog.bcmall</groupId> | ||
<artifactId>bc-utils</artifactId> | ||
<version>0.9.9</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>cn.xjjdog.bcmall</groupId> | ||
<artifactId>module-crm</artifactId> | ||
<version>0.9.9</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>cn.xjjdog.bcmall</groupId> | ||
<artifactId>module-imagesrv</artifactId> | ||
<version>0.9.9</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>cn.xjjdog.bcmall</groupId> | ||
<artifactId>module-order</artifactId> | ||
<version>0.9.9</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>cn.xjjdog.bcmall</groupId> | ||
<artifactId>module-product</artifactId> | ||
<version>0.9.9</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>cn.xjjdog.bcmall</groupId> | ||
<artifactId>module-payment</artifactId> | ||
<version>0.9.9</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<configuration> | ||
<excludeDevtools>false</excludeDevtools> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>org.mapstruct</groupId> | ||
<artifactId>mapstruct-processor</artifactId> | ||
<version>${org.mapstruct.version}</version> | ||
</path> | ||
<path> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.16</version> | ||
</path> | ||
<path> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok-mapstruct-binding</artifactId> | ||
<version>0.2.0</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cn.xjjdog.bcmall; | ||
|
||
import cn.xjjdog.bcmall.module.crm.persistence.MemberEntity; | ||
import cn.xjjdog.bcmall.module.order.persistence.ExpressCompanyEntity; | ||
import cn.xjjdog.bcmall.module.product.persistence.BrandEntity; | ||
import cn.xjjdog.bcmall.module.product.persistence.ProductCategoryEntity; | ||
import cn.xjjdog.bcmall.module.product.persistence.ProductUnitEntity; | ||
import cn.xjjdog.bcmall.utils.quickdev.magicjpa.MagicJpaController; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* Copyright (c) 2021. All Rights Reserved. | ||
* | ||
* @author xjjdog | ||
*/ | ||
@SpringBootApplication | ||
public class App { | ||
|
||
static { | ||
MagicJpaController.register("ProductCategory", ProductCategoryEntity.class); | ||
MagicJpaController.register("Brand", BrandEntity.class); | ||
MagicJpaController.register("ProductUnit", ProductUnitEntity.class); | ||
MagicJpaController.register("Member", MemberEntity.class); | ||
MagicJpaController.register("Express", ExpressCompanyEntity.class); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(App.class, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cn.xjjdog.bcmall; | ||
|
||
/** | ||
* Copyright (c) 2021. All Rights Reserved. | ||
* | ||
* @author xjjdog | ||
* @Description TODO | ||
*/ | ||
public class BizException extends RuntimeException { | ||
public BizException(String msg) { | ||
super(msg); | ||
} | ||
public BizException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
bc-admin/src/main/java/cn/xjjdog/bcmall/ConfigException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cn.xjjdog.bcmall; | ||
|
||
/** | ||
* Copyright (c) 2021. All Rights Reserved. | ||
* | ||
* @author xjjdog | ||
* @Description TODO | ||
*/ | ||
public class ConfigException extends RuntimeException { | ||
public ConfigException(String msg) { | ||
super(msg); | ||
} | ||
public ConfigException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
bc-admin/src/main/java/cn/xjjdog/bcmall/auth/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cn.xjjdog.bcmall.auth; | ||
|
||
import cn.xjjdog.bcmall.auth.request.LoginRequest; | ||
import cn.xjjdog.bcmall.auth.request.RenewRequest; | ||
import cn.xjjdog.bcmall.auth.response.Token; | ||
import cn.xjjdog.bcmall.auth.response.UserInfo; | ||
import cn.xjjdog.bcmall.utils.web.Result; | ||
import com.google.common.base.Preconditions; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
|
||
/** | ||
* Copyright (c) 2021. All Rights Reserved. | ||
* <p> | ||
* 用户验证接口 | ||
* | ||
* @author xjjdog | ||
*/ | ||
@RestController | ||
@RequestMapping("/api/auth/") | ||
@CrossOrigin | ||
@Api("用户验证接口") | ||
public class AuthController { | ||
|
||
@Autowired | ||
private AuthenticationManager authenticationManager; | ||
|
||
@Autowired | ||
private JwtTools jwt; | ||
|
||
@ApiOperation("获取当前用户信息") | ||
@GetMapping("currentUser") | ||
public UserInfo currentUser() throws Exception { | ||
UserInfo userInfo = new UserInfo(); | ||
userInfo.setAvatar("http://xjjdog.cn/pkq.jpeg"); | ||
userInfo.setName("小姐姐味道"); | ||
return userInfo; | ||
} | ||
|
||
@ApiOperation("令牌续租接口") | ||
@PostMapping("renewToken") | ||
public Result<?> renew(@RequestBody RenewRequest renewReq) { | ||
String newToken = jwt.renew(renewReq.getOldToken()); | ||
return Result.of(new Token(newToken)); | ||
} | ||
|
||
|
||
@ApiOperation("获取令牌") | ||
@PostMapping("getToken") | ||
public Result<?> login(@RequestBody LoginRequest loginRequest) { | ||
final String username = loginRequest.getUsername(); | ||
final String password = loginRequest.getPassword(); | ||
|
||
Preconditions.checkNotNull(username); | ||
Preconditions.checkNotNull(password); | ||
|
||
Authentication authentication = authenticate(username, password); | ||
User user = User.class.cast(authentication.getPrincipal()); | ||
|
||
List<String> roles = user.getAuthorities() | ||
.stream() | ||
.map(v -> v.getAuthority()) | ||
.collect(Collectors.toList()); | ||
|
||
Map map = new HashMap<>(); | ||
map.put("roles", roles); | ||
|
||
final String token = jwt.generateToken(map, username); | ||
|
||
return Result.of(new Token(token)); | ||
} | ||
|
||
/** | ||
* 调用密码验证方法,这里使用Spring Security的用户管理方法 | ||
* | ||
* @param username 用户名 | ||
* @param password 用户密码 | ||
* @return 验证后的 Authentication | ||
*/ | ||
private Authentication authenticate(String username, String password) { | ||
return authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password)); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
bc-admin/src/main/java/cn/xjjdog/bcmall/auth/AuthFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package cn.xjjdog.bcmall.auth; | ||
|
||
import cn.xjjdog.bcmall.utils.utils.SpringContextUtil; | ||
import io.jsonwebtoken.Claims; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Copyright (c) 2021. All Rights Reserved. | ||
* <p> | ||
* 注意,我们的Filter并未受到Spring的管理,这是因为我们需要把它加入到过滤器链中。 <br/> | ||
* 如果被Spring管理,将会产生很多不可用预料的行为 | ||
* | ||
* @author xjjdog | ||
*/ | ||
@Slf4j | ||
public class AuthFilter extends OncePerRequestFilter { | ||
|
||
/** | ||
* 请勿直接引用,否则会绕过懒加载 | ||
*/ | ||
private JwtTools jwt; | ||
|
||
/** | ||
* @return 懒加载获取JwtTools | ||
*/ | ||
private JwtTools getJwt() { | ||
if (null == jwt) { | ||
jwt = SpringContextUtil.getBean(JwtTools.class); | ||
} | ||
return jwt; | ||
} | ||
|
||
/** | ||
* 请求拦截过滤器实现。 | ||
* 系统要求每一个受限资源,都需要提供一个名称叫做Authorization的Http头。当提供了错误的Token,或者没有提供Token,将会拒绝访问 <br/> | ||
* Token验证通过后,会在SecurityContextHolder中保持这些登录信息。由于SecurityContextHolder是ThreadLocal,所以在本次请求<br/> | ||
* 的生命周期中,可持续获取 | ||
*/ | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) | ||
throws ServletException, IOException { | ||
|
||
final String token = request.getHeader("Authorization"); | ||
if (!StringUtils.hasLength(token)) { | ||
log.debug("{} | request with empty token", request.getRequestURI()); | ||
chain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
// 一般都会解析成功,除非瞎填,或者外部攻击 | ||
Claims claims; | ||
try { | ||
claims = getJwt().getClaims(token); | ||
} catch (Exception ex) { | ||
log.error("JWT Token error: {} , cause: {}", token, ex.getMessage()); | ||
chain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
// 通过Token里的信息,构造验证信息,无需再与数据库进行交互; 这意味着,如果用户的权限变更,它需要重新登录 | ||
if (SecurityContextHolder.getContext().getAuthentication() == null) { | ||
boolean ok = getJwt().validateTokenExpiration(claims); | ||
if (ok) { | ||
String name = claims.getSubject(); | ||
List<GrantedAuthority> roles = (List<GrantedAuthority>) List.class.cast(claims.get("roles")). | ||
stream() | ||
.map(v -> new SimpleGrantedAuthority(String.valueOf(v))) | ||
.collect(Collectors.toList()); | ||
UserDetails userDetails = new User(name, "EMPTY", roles); | ||
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken( | ||
userDetails, | ||
null, | ||
userDetails.getAuthorities()); | ||
authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); | ||
SecurityContextHolder.getContext().setAuthentication(authToken); | ||
} | ||
} | ||
chain.doFilter(request, response); | ||
} | ||
} |
Oops, something went wrong.