前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于Spring Cloud 几行配置完成单点登录开发

基于Spring Cloud 几行配置完成单点登录开发

原创
作者头像
冷冷
修改2018-01-29 09:30:30
9070
修改2018-01-29 09:30:30
举报
文章被收录于专栏:冷冷
image
image

单点登录概念

单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一。SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。登录逻辑如上图

基于Spring 全家桶的实现

技术选型:

代码语言:txt
复制
Spring Boot

Spring Cloud 

Spring Security oAuth2

客户端:

maven依赖

代码语言:txt
复制
<dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-security</artifactId>

</dependency>

<dependency>

    <groupId>org.springframework.security.oauth</groupId>

    <artifactId>spring-security-oauth2</artifactId>

</dependency>

<dependency>

    <groupId>org.springframework.security</groupId>

    <artifactId>spring-security-jwt</artifactId>

</dependency>

EnableOAuth2Sso 注解

入口类配置@@EnableOAuth2Sso

代码语言:txt
复制
@SpringBootApplication

public class PigSsoClientDemoApplication {



    public static void main(String[] args) {

        SpringApplication.run(PigSsoClientDemoApplication.class, args);

    }



}

配置文件

代码语言:txt
复制
security:

  oauth2:

    client:

      client-id: pig

      client-secret: pig

      user-authorization-uri: http://localhost:3000/oauth/authorize

      access-token-uri: http://localhost:3000/oauth/token

      scope: serve

    resource:

      jwt:

        key-uri: http://localhost:3000/oauth/token\_key

  sessions: neve

SSO认证服务器

认证服务器配置

代码语言:txt
复制
@Configuration

@Order(Integer.MIN\_VALUE)

@EnableAuthorizationServe

public class PigAuthorizationConfig extends AuthorizationServerConfigurerAdapter {



    @Override

    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemory()

                .withClient(authServerConfig.getClientId())

                .secret(authServerConfig.getClientSecret())

                .authorizedGrantTypes(SecurityConstants.REFRESH\_TOKEN, SecurityConstants.PASSWORD,SecurityConstants.AUTHORIZATION\_CODE)

                .scopes(authServerConfig.getScope());

    }



    @Override

    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {

        endpoints

                .tokenStore(new RedisTokenStore(redisConnectionFactory))

                .accessTokenConverter(jwtAccessTokenConverter())

                .authenticationManager(authenticationManager)

                .exceptionTranslator(pigWebResponseExceptionTranslator)

                .reuseRefreshTokens(false)

                .userDetailsService(userDetailsService);

    }



    @Override

    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

        security

                .allowFormAuthenticationForClients()

                .tokenKeyAccess("isAuthenticated()")

                .checkTokenAccess("permitAll()");

    }



    @Bean

    public PasswordEncoder passwordEncoder() {

        return new BCryptPasswordEncoder();

    }



    @Bean

    public JwtAccessTokenConverter jwtAccessTokenConverter() {

        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();

        jwtAccessTokenConverter.setSigningKey(CommonConstant.SIGN\_KEY);

        return jwtAccessTokenConverter;

    }



}

配置完成体验

  1. 访问SSO客户端的 index.html
  2. 重定向到SSO服务端的 Basic 认证
  3. 输入账号密码又重定向到原请求的 客户端index资源

总结

  • 客户端访问服务端 403问题? 用户需要拥有ROLE_USER的权限,具体的可以通过日志可以查看到报错。
  • Possible CSRF detected - state parameter was present but no state could be found 目前是通过设置session: never或者 cotext-path解决
  • 源码请参考 https://gitee.com/log4j/ 基于Spring Cloud、Spring Security Oauth2.0开发企业级认证与授权,提供常见服务监控、链路追踪、日志分析、缓存管理、任务调度等实现

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 单点登录概念
  • 基于Spring 全家桶的实现
    • 客户端:
      • EnableOAuth2Sso 注解
        • 配置文件
        • SSO认证服务器
          • 认证服务器配置
          • 配置完成体验
          • 总结
          相关产品与服务
          Elasticsearch Service
          腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档