Skip to content

Commit b947b91

Browse files
committed
refactor/认证中心重构
1 parent 7c0d8a3 commit b947b91

File tree

22 files changed

+615
-1053
lines changed

22 files changed

+615
-1053
lines changed

pmhub-api/pmhub-api-system/src/main/java/com/laigeoffer/pmhub/api/system/UserFeignService.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.laigeoffer.pmhub.api.system;
22

33
import com.laigeoffer.pmhub.api.system.factory.UserFeginFallbackFactory;
4+
import com.laigeoffer.pmhub.base.core.constant.SecurityConstants;
45
import com.laigeoffer.pmhub.base.core.constant.ServiceNameConstants;
56
import com.laigeoffer.pmhub.base.core.core.domain.AjaxResult;
67
import org.springframework.cloud.openfeign.FeignClient;
78
import org.springframework.web.bind.annotation.GetMapping;
89
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestHeader;
911

1012
/**
1113
* @author canghe
@@ -18,6 +20,12 @@ public interface UserFeignService {
1820
/**
1921
* 根据用户编号获取详细信息
2022
*/
21-
@GetMapping(value = {"/", "/{userId}"})
22-
AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId) ;
23+
@GetMapping(value = { "/user/{userId}"})
24+
AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
25+
26+
/**
27+
* 根据用户名获取当前用户信息
28+
*/
29+
@GetMapping("/user/info/{username}")
30+
AjaxResult getInfoByUsername(@PathVariable("username") String username);
2331
}

pmhub-api/pmhub-api-system/src/main/java/com/laigeoffer/pmhub/api/system/factory/UserFeginFallbackFactory.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ public UserFeignService create(Throwable throwable)
2424
return new UserFeignService()
2525
{
2626
@Override
27-
public AjaxResult getInfo(Long userId)
28-
{
27+
public AjaxResult getInfo(Long userId, String source) {
28+
return AjaxResult.error("获取用户失败:" + throwable.getMessage());
29+
}
30+
31+
@Override
32+
public AjaxResult getInfoByUsername(String username) {
2933
return AjaxResult.error("获取用户失败:" + throwable.getMessage());
3034
}
3135

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2+
com.laigeoffer.pmhub.api.system.factory.UserFeginFallbackFactory

pmhub-auth/pom.xml

+1-5
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,14 @@
6565
<artifactId>pmhub-base-security</artifactId>
6666
</dependency>
6767

68-
<!-- <dependency>-->
69-
<!-- <groupId>com.laigeoffer.pmhub-cloud</groupId>-->
70-
<!-- <artifactId>pmhub-base-framework</artifactId>-->
71-
<!-- </dependency>-->
72-
7368
<dependency>
7469
<groupId>com.laigeoffer.pmhub-cloud</groupId>
7570
<artifactId>pmhub-api-system</artifactId>
7671
</dependency>
7772

7873

7974

75+
8076
</dependencies>
8177

8278

pmhub-auth/src/main/java/com/laigeoffer/pmhub/auth/PmHubAuthApplication.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.laigeoffer.pmhub.auth;
22

3+
import com.laigeoffer.pmhub.base.security.annotation.EnablePmFeignClients;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
56
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@@ -9,7 +10,7 @@
910
* @description 认证授权中心
1011
* @create 2024-04-23-15:00
1112
*/
12-
//@EnablePmFeignClients todo
13+
@EnablePmFeignClients
1314
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
1415
public class PmHubAuthApplication {
1516
public static void main(String[] args) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.laigeoffer.pmhub.auth.config;
2+
3+
import com.laigeoffer.pmhub.auth.handle.AuthenticationEntryPointImpl;
4+
import com.laigeoffer.pmhub.base.security.filter.JwtAuthenticationTokenFilter;
5+
import com.laigeoffer.pmhub.base.security.handle.LogoutSuccessHandlerImpl;
6+
import com.laigeoffer.pmhub.base.security.properties.PermitAllUrlProperties;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.http.HttpMethod;
10+
import org.springframework.security.authentication.AuthenticationManager;
11+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
12+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
13+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
14+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
15+
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
16+
import org.springframework.security.config.http.SessionCreationPolicy;
17+
import org.springframework.security.core.userdetails.UserDetailsService;
18+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
19+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
20+
import org.springframework.security.web.authentication.logout.LogoutFilter;
21+
import org.springframework.web.filter.CorsFilter;
22+
23+
/**
24+
* spring security配置
25+
*
26+
* @author canghe
27+
*/
28+
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
29+
public class SecurityConfig extends WebSecurityConfigurerAdapter {
30+
/**
31+
* 自定义用户认证逻辑
32+
*/
33+
@Autowired
34+
private UserDetailsService userDetailsService;
35+
36+
/**
37+
* 认证失败处理类
38+
*/
39+
@Autowired
40+
private AuthenticationEntryPointImpl unauthorizedHandler;
41+
42+
/**
43+
* 退出处理类
44+
*/
45+
@Autowired
46+
private LogoutSuccessHandlerImpl logoutSuccessHandler;
47+
48+
/**
49+
* token认证过滤器
50+
*/
51+
@Autowired
52+
private JwtAuthenticationTokenFilter authenticationTokenFilter;
53+
54+
/**
55+
* 跨域过滤器
56+
*/
57+
@Autowired
58+
private CorsFilter corsFilter;
59+
60+
/**
61+
* 允许匿名访问的地址
62+
*/
63+
@Autowired
64+
private PermitAllUrlProperties permitAllUrl;
65+
66+
/**
67+
* 解决 无法直接注入 AuthenticationManager
68+
*
69+
* @return
70+
* @throws Exception
71+
*/
72+
@Bean
73+
@Override
74+
public AuthenticationManager authenticationManagerBean() throws Exception {
75+
return super.authenticationManagerBean();
76+
}
77+
78+
/**
79+
* anyRequest | 匹配所有请求路径
80+
* access | SpringEl表达式结果为true时可以访问
81+
* anonymous | 匿名可以访问
82+
* denyAll | 用户不能访问
83+
* fullyAuthenticated | 用户完全认证可以访问(非remember-me下自动登录)
84+
* hasAnyAuthority | 如果有参数,参数表示权限,则其中任何一个权限可以访问
85+
* hasAnyRole | 如果有参数,参数表示角色,则其中任何一个角色可以访问
86+
* hasAuthority | 如果有参数,参数表示权限,则其权限可以访问
87+
* hasIpAddress | 如果有参数,参数表示IP地址,如果用户IP和参数匹配,则可以访问
88+
* hasRole | 如果有参数,参数表示角色,则其角色可以访问
89+
* permitAll | 用户可以任意访问
90+
* rememberMe | 允许通过remember-me登录的用户访问
91+
* authenticated | 用户登录后可访问
92+
*/
93+
@Override
94+
protected void configure(HttpSecurity httpSecurity) throws Exception {
95+
// 注解标记允许匿名访问的url
96+
ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = httpSecurity.authorizeRequests();
97+
permitAllUrl.getUrls().forEach(url -> registry.antMatchers(url).permitAll());
98+
99+
httpSecurity
100+
// CSRF禁用,因为不使用session
101+
.csrf().disable()
102+
// 禁用HTTP响应标头
103+
.headers().cacheControl().disable().and()
104+
// 认证失败处理类
105+
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
106+
// 基于token,所以不需要session
107+
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
108+
// 过滤请求
109+
.authorizeRequests()
110+
// 对于外部oa调用 单点登录 登录login 注册register 验证码captchaImage 允许匿名访问
111+
.antMatchers("WW_verify_demRgYMCtoo6EQlR.txt","/WW_verify_BewKb7YjhLQkBDPD.txt","/oa/**","/sso/wx/**","/sso/oauth2/accessToken","/sso/oauth2/user", "/sso/oa/**","/login", "/register", "/captchaImage", "/websocket/**").permitAll()
112+
// 静态资源,可匿名访问
113+
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
114+
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
115+
// 除上面外的所有请求全部需要鉴权认证
116+
.anyRequest().authenticated()
117+
.and()
118+
.headers().frameOptions().disable();
119+
// 添加Logout filter
120+
httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
121+
// 添加JWT filter
122+
httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
123+
// 添加CORS filter
124+
httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
125+
httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);
126+
}
127+
128+
/**
129+
* 强散列哈希加密实现
130+
*/
131+
@Bean
132+
public BCryptPasswordEncoder bCryptPasswordEncoder() {
133+
return new BCryptPasswordEncoder();
134+
}
135+
136+
/**
137+
* 身份认证接口
138+
*/
139+
@Override
140+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
141+
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
142+
}
143+
}
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,86 @@
1-
//package com.laigeoffer.pmhub.auth.controller;
2-
//
3-
//import com.laigeoffer.pmhub.api.system.UserFeignService;
4-
//import com.laigeoffer.pmhub.base.core.core.domain.AjaxResult;
5-
//import com.laigeoffer.pmhub.base.core.utils.JsonUtils;
6-
//import lombok.extern.slf4j.Slf4j;
7-
//import org.springframework.web.bind.annotation.GetMapping;
8-
//import org.springframework.web.bind.annotation.PathVariable;
9-
//import org.springframework.web.bind.annotation.RestController;
10-
//
11-
//import javax.annotation.Resource;
12-
//
13-
//
14-
///**
15-
// * 登录验证
16-
// *
17-
// * @author canghe
18-
// */
19-
//@RestController
20-
//@Slf4j
21-
//public class LoginController {
22-
//
23-
//
24-
//// @Autowired
25-
//// private SysLoginService loginService;
26-
//
27-
// @Resource
28-
// private UserFeignService userFeignService;
29-
//
30-
//// @Autowired
31-
//// private ISysMenuService menuService;
32-
////
33-
////
34-
//// @Autowired
35-
//// private SysPermissionService permissionService;
36-
//
37-
//
38-
//
39-
//
40-
//
41-
//
42-
//// /**
43-
//// * 登录方法
44-
//// *
45-
//// * @param loginBody 登录信息
46-
//// * @return 结果
47-
//// */
48-
//// @PostMapping("/login")
49-
//// public AjaxResult login(@RequestBody LoginBody loginBody) {
50-
//// AjaxResult ajax = success();
51-
//// // 生成令牌
52-
//// String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
53-
//// loginBody.getUuid());
54-
//// ajax.put(Constants.TOKEN, token);
55-
//// return ajax;
56-
//// }
57-
////
1+
package com.laigeoffer.pmhub.auth.controller;
2+
3+
import com.laigeoffer.pmhub.auth.service.SysLoginService;
4+
import com.laigeoffer.pmhub.base.core.constant.Constants;
5+
import com.laigeoffer.pmhub.base.core.core.domain.AjaxResult;
6+
import com.laigeoffer.pmhub.base.core.core.domain.model.LoginBody;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import static com.laigeoffer.pmhub.base.core.core.domain.AjaxResult.success;
13+
14+
/**
15+
* 登录验证
16+
*
17+
* @author canghe
18+
*/
19+
@RestController
20+
public class LoginController {
21+
22+
23+
@Autowired
24+
private SysLoginService loginService;
25+
26+
// @Autowired
27+
// private ISysMenuService menuService;
28+
//
29+
//
30+
// @Autowired
31+
// private SysPermissionService permissionService;
32+
33+
34+
35+
36+
37+
38+
/**
39+
* 登录方法
40+
*
41+
* @param loginBody 登录信息
42+
* @return 结果
43+
*/
44+
@PostMapping("/login")
45+
public AjaxResult login(@RequestBody LoginBody loginBody) {
46+
AjaxResult ajax = success();
47+
// 生成令牌
48+
String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
49+
loginBody.getUuid());
50+
ajax.put(Constants.TOKEN, token);
51+
return ajax;
52+
}
53+
5854
// /**
5955
// * 获取用户信息
6056
// *
6157
// * @return 用户信息
6258
// */
63-
// @GetMapping("getInfo/{userid}")
64-
// public AjaxResult getInfo(@PathVariable long userid) {
65-
//// SysUser user = SecurityUtils.getLoginUser().getUser();
66-
// AjaxResult userInfo = userFeignService.getInfo(userid);
67-
// log.info(JsonUtils.toJsonString(userInfo));
68-
// return userInfo;
59+
// @GetMapping("getInfo")
60+
// public AjaxResult getInfo() {
61+
// SysUser user = SecurityUtils.getLoginUser().getUser();
62+
// // 角色集合
63+
// Set<String> roles = permissionService.getRolePermission(user);
64+
// // 权限集合
65+
// Set<String> permissions = permissionService.getMenuPermission(user);
66+
// AjaxResult ajax = success();
67+
// ajax.put("user", user);
68+
// ajax.put("roles", roles);
69+
// ajax.put("permissions", permissions);
70+
// return ajax;
6971
// }
70-
////
71-
//// /**
72-
//// * 获取路由信息
73-
//// *
74-
//// * @return 路由信息
75-
//// */
76-
//// @GetMapping("getRouters")
77-
//// public AjaxResult getRouters() {
78-
//// Long userId = SecurityUtils.getUserId();
79-
//// List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
80-
//// return success(menuService.buildMenus(menus));
81-
//// }
82-
//
8372
//
84-
//
85-
//}
73+
// /**
74+
// * 获取路由信息
75+
// *
76+
// * @return 路由信息
77+
// */
78+
// @GetMapping("getRouters")
79+
// public AjaxResult getRouters() {
80+
// Long userId = SecurityUtils.getUserId();
81+
// List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
82+
// return success(menuService.buildMenus(menus));
83+
// }
84+
85+
86+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.laigeoffer.pmhub.base.security.handle;
1+
package com.laigeoffer.pmhub.auth.handle;
22

33
import com.alibaba.fastjson2.JSON;
44
import com.laigeoffer.pmhub.base.core.constant.HttpStatus;

0 commit comments

Comments
 (0)