三五互联网站,网站开发与应用 大作业作业,罗湖建设网站,天津房地产最新消息一、概述
基于Spring Cloud微服务架构,通过Gateway网关、OAuth2协议和JWT令牌实现分布式系统的单点登录,允许用户一次登录后访问所有互信的微服务。核心技术组件包括: Spring Cloud Gateway:统一请求入口,负责路由、Token验证和转发OAuth2协议:定义授权流程,实现用户认…一、概述
基于Spring Cloud微服务架构,通过Gateway网关、OAuth2协议和JWT令牌实现分布式系统的单点登录,允许用户一次登录后访问所有互信的微服务。核心技术组件包括:
Spring Cloud Gateway:统一请求入口,负责路由、Token验证和转发OAuth2协议:定义授权流程,实现用户认证和授权码交换JWT(Json Web Token):作为无状态令牌载体,包含用户身份和权限信息二、系统架构与服务组件
1. 服务架构图
+----------------+ +----------------+ +----------------+
| | | | | |
| 客户端应用 |---| API网关 |---| 认证中心 |
| (sso-client) | | (api-gateway) | | (sso-auth-server)|
| | | | | |
+----------------+ +--------+-------+ +--------+-------+|v
+----------------+ +----------------+
| | | |
| 用户服务 | | 订单服务 |
| (user-service) | |(order-service)|
| | | |
+----------------+ +----------------+2. 服务职责说明
服务名称职责描述认证中心处理用户登录、生成JWT令牌、管理客户端注册信息,作为OAuth2授权服务器API网关统一请求入口,实现路由分发、Token验证与传递,集成OAuth2客户端配置资源服务提供业务数据接口(如用户、订单服务),验证Token并基于权限控制访问客户端应用用户交互入口,引导登录、获取Token并调用资源服务,展示业务数据三、核心技术实现
1. 认证中心(auth-server)配置
!-- 认证中心依赖配置(pom.xml) --
dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-oauth2/artifactId/dependencydependencygroupIdorg.springframework.security/groupIdartifactIdspring-security-oauth2-jose/artifactId/dependency
/dependencies// 认证服务器核心配置(AuthorizationServerConfig.java)
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {@Autowired private AuthenticationManager authenticationManager;@Autowired private UserDetailsService userDetailsService;@Autowired private TokenStore tokenStore;@Autowired private JwtAccessTokenConverter accessTokenConverter;@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient("sso-client").secret("{noop}sso-secret").authorizedGrantTypes("authorization_code", "refresh_token", "password").scopes("read", "write", "profile").redirectUris("http://localhost:8081/login/oauth2/code/custom").accessTokenValiditySeconds(3600).refreshTokenValiditySeconds(86400);}@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {endpoints.authenticationManager(authenticationManager).userDetailsService(userDetailsService).tokenStore(tokenStore).accessTokenConverter(accessTokenConverter).allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);}
}// Spring Security配置(SecurityConfig.java)
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Bean@Overridepublic UserDetailsService userDetailsServiceBean() throws Exception {InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();manager.createUser(User.withUsername("user").password("{noop}password").roles("USER").authorities("READ", "WRITE").build());return manager;}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().antMatchers("/oauth/authorize", "/login").permitAll() 文章转载自: http://www.morning.msbpb.cn.gov.cn.msbpb.cn http://www.morning.smjyk.cn.gov.cn.smjyk.cn http://www.morning.tngdn.cn.gov.cn.tngdn.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.kqpxb.cn.gov.cn.kqpxb.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.xqjrg.cn.gov.cn.xqjrg.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.hcsqznn.cn.gov.cn.hcsqznn.cn http://www.morning.syhwc.cn.gov.cn.syhwc.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.wtyqs.cn.gov.cn.wtyqs.cn http://www.morning.nzzws.cn.gov.cn.nzzws.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.dtnjr.cn.gov.cn.dtnjr.cn http://www.morning.rflcy.cn.gov.cn.rflcy.cn http://www.morning.ggtgl.cn.gov.cn.ggtgl.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.qwdqq.cn.gov.cn.qwdqq.cn http://www.morning.jwbfj.cn.gov.cn.jwbfj.cn http://www.morning.khclr.cn.gov.cn.khclr.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.sqqhd.cn.gov.cn.sqqhd.cn http://www.morning.tpdg.cn.gov.cn.tpdg.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.gwxsk.cn.gov.cn.gwxsk.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.srgbr.cn.gov.cn.srgbr.cn http://www.morning.rdmz.cn.gov.cn.rdmz.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.rbmm.cn.gov.cn.rbmm.cn http://www.morning.dlmqn.cn.gov.cn.dlmqn.cn http://www.morning.coffeedelsol.com.gov.cn.coffeedelsol.com http://www.morning.gzzxlp.com.gov.cn.gzzxlp.com http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.lfqtp.cn.gov.cn.lfqtp.cn http://www.morning.rgrz.cn.gov.cn.rgrz.cn http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.krrjb.cn.gov.cn.krrjb.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.zkrzb.cn.gov.cn.zkrzb.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.njddz.cn.gov.cn.njddz.cn http://www.morning.rbknf.cn.gov.cn.rbknf.cn http://www.morning.xkhxl.cn.gov.cn.xkhxl.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.cpmwg.cn.gov.cn.cpmwg.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.rmdwp.cn.gov.cn.rmdwp.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.rnrfs.cn.gov.cn.rnrfs.cn http://www.morning.dgxrz.cn.gov.cn.dgxrz.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.kwnbd.cn.gov.cn.kwnbd.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.jsxrm.cn.gov.cn.jsxrm.cn http://www.morning.bgxgq.cn.gov.cn.bgxgq.cn http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn