资讯专栏INFORMATION COLUMN

Spring Security 单点登录简单示例

Miracle / 1801人阅读

摘要:目标认证服务器认证后获取,客户端访问资源时带上进行安全验证。关键依赖认证服务器认证服务器的关键代码有如下几个文件认证配置配置客户端允许表单认证代码中配置了一个,是,密码。配置了单点登录。

本文为[原创]文章,转载请标明出处。
本文链接:https://weyunx.com/2019/02/12...
本文出自微云的技术博客
Overview

最近在弄单点登录,踩了不少坑,所以记录一下,做了个简单的例子。

目标:认证服务器认证后获取 token,客户端访问资源时带上 token 进行安全验证。

可以直接看源码。

关键依赖

        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
        



        
            org.springframework.boot
            spring-boot-starter-security
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.security
            spring-security-test
            test
        
        
            org.springframework.security.oauth.boot
            spring-security-oauth2-autoconfigure
            2.1.2.RELEASE
        
认证服务器

认证服务器的关键代码有如下几个文件:

AuthServerApplication:

@SpringBootApplication
@EnableResourceServer
public class AuthServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(AuthServerApplication.class, args);
    }

}

AuthorizationServerConfiguration 认证配置:

@Configuration
@EnableAuthorizationServer
class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
    @Autowired
    AuthenticationManager authenticationManager;

    @Autowired
    TokenStore tokenStore;

    @Autowired
    BCryptPasswordEncoder encoder;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //配置客户端
        clients
                .inMemory()
                .withClient("client")
                .secret(encoder.encode("123456")).resourceIds("hi")
                .authorizedGrantTypes("password","refresh_token")
                .scopes("read");
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .tokenStore(tokenStore)
                .authenticationManager(authenticationManager);
    }


    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        //允许表单认证
        oauthServer
                .allowFormAuthenticationForClients()
                .checkTokenAccess("permitAll()")
                .tokenKeyAccess("permitAll()");
    }
}

代码中配置了一个 client,id 是 client,密码 123456authorizedGrantTypespasswordrefresh_token 两种方式。

SecurityConfiguration 安全配置:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Bean
    public TokenStore tokenStore() {
        return new InMemoryTokenStore();
    }

    @Bean
    public BCryptPasswordEncoder encoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
               .passwordEncoder(encoder())
               .withUser("user_1").password(encoder().encode("123456")).roles("USER")
               .and()
               .withUser("user_2").password(encoder().encode("123456")).roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http.csrf().disable()
                .requestMatchers()
                .antMatchers("/oauth/authorize")
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin().permitAll();
        // @formatter:on
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


}

上面在内存中创建了两个用户,角色分别是 USERADMIN。后续可考虑在数据库或者 Redis 中存储相关信息。

AuthUser 配置获取用户信息的 Controller:

@RestController
public class AuthUser {
        @GetMapping("/oauth/user")
        public Principal user(Principal principal) {
            return principal;
        }

}

application.yml 配置,主要就是配置个端口号:

---
spring:
  profiles:
    active: dev
  application:
    name: auth-server
server:
  port: 8101
客户端配置

客户端的配置比较简单,主要代码结构如下:

application.yml 配置:

---
spring:
  profiles:
    active: dev
  application:
    name: client

server:
  port: 8102
security:
  oauth2:
    client:
      client-id: client
      client-secret: 123456
      access-token-uri: http://localhost:8101/oauth/token
      user-authorization-uri: http://localhost:8101/oauth/authorize
      scope: read
      use-current-uri: false
    resource:
      user-info-uri: http://localhost:8101/oauth/user

这里主要是配置了认证服务器的相关地址以及客户端的 id 和 密码。user-info-uri 配置的就是服务器端获取用户信息的接口。

HelloController 访问的资源,配置了 ADMIN 的角色才可以访问:

@RestController
public class HelloController {
    @RequestMapping("/hi")
    @PreAuthorize("hasRole("ADMIN")")
    public ResponseEntity hi() {
        return ResponseEntity.ok().body("auth success!");
    }
}

WebSecurityConfiguration 相关安全配置:

@Configuration
@EnableOAuth2Sso
@EnableGlobalMethodSecurity(prePostEnabled = true) 
class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {

        http
                .csrf().disable()
                // 基于token,所以不需要session
              .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .anyRequest().authenticated();
    }


}

其中 @EnableGlobalMethodSecurity(prePostEnabled = true) 开启后,Spring Security 的 @PreAuthorize,@PostAuthorize 注解才可以使用。

@EnableOAuth2Sso 配置了单点登录。

ClientApplication

@SpringBootApplication
@EnableResourceServer
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

}
验证

启动项目后,我们使用 postman 来进行验证。

首先是获取 token:

选择 POST 提交,地址为验证服务器的地址,参数中输入 username,password,grant_typescope ,其中 grant_type 需要输入 password

然后在下面等 Authorization 标签页中,选择 Basic Auth,然后输入 client 的 id 和 password。

{
    "access_token": "02f501a9-c482-46d4-a455-bf79a0e0e728",
    "token_type": "bearer",
    "refresh_token": "0e62ffffdc-4f51-4cb5-81c3-5383fddbb81b",
    "expires_in": 41741,
    "scope": "read"
}

此时就可以获得 access_token 为: 02f501a9-c482-46d4-a455-bf79a0e0e728。需要注意的是这里是用 user_2 获取的 token,即角色是 ADMIN

然后我们再进行获取资源的验证:

使用 GET 方法,参数中输入 access_token,值输入 02f501a9-c482-46d4-a455-bf79a0e0e728

点击提交后即可获取到结果。

如果我们不加上 token ,则会提示无权限。同样如果我们换上 user_1 获取的 token,因 user_1 的角色是 USER,此资源需要 ADMIN 权限,则此处还是会获取失败。

简单的例子就到这,后续有时间再加上其它功能吧,谢谢~

未完待续...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73467.html

相关文章

  • 基于spring-security-oauth2实现单点登录(持续更新)

    摘要:认证服务器和浏览器控制台也没有报错信息。这里简单介绍下如何查阅源码,首先全局搜索自己的配置因为这个地址是认证服务器请求授权的,所以,请求认证的过滤器肯定包含他。未完待续,下一篇介绍资源服务器和认证服务器的集成。 基于spring-security-oauth2-实现单点登录 文章代码地址:链接描述可以下载直接运行,基于springboot2.1.5,springcloud Green...

    zhongmeizhi 评论0 收藏0
  • 基于spring-security-oauth2实现单点登录(持续更新)

    摘要:认证服务器和浏览器控制台也没有报错信息。这里简单介绍下如何查阅源码,首先全局搜索自己的配置因为这个地址是认证服务器请求授权的,所以,请求认证的过滤器肯定包含他。未完待续,下一篇介绍资源服务器和认证服务器的集成。 基于spring-security-oauth2-实现单点登录 文章代码地址:链接描述可以下载直接运行,基于springboot2.1.5,springcloud Green...

    妤锋シ 评论0 收藏0
  • 门户(Portal)安全(Security单点登录(SSO)文档说明

    摘要:作为单点登录,实现了架构。用户在的页面上进行登录操作。需要使用单点登录的各个服务,相当于上文中的产品。用于验证用户是否登录中心服务器。当请求到来时,以此值为查询缓存中有无,如果有的话,则相信用户已登录过。是为用户签发的访问某一的票据。 ...

    Kaede 评论0 收藏0
  • 单点登录与消息队列

    摘要:前言很久都没有写博客了,这次为大家简单介绍两个在开发中经常使用的概念单点登录和消息队列以及具体到中的一些实现方案。单点登录的实质就是安全上下文或凭证在多个应用系统之间的传递或共享。 前言 很久都没有写博客了,这次为大家简单介绍两个在WEB开发中经常使用的概念——单点登录和消息队列以及具体到J2EE中的一些实现方案。本文原创性的工作比较少,主要是一些总结概括和自己的理解。 单点登录SS...

    Jioby 评论0 收藏0
  • 单点登录与消息队列

    摘要:前言很久都没有写博客了,这次为大家简单介绍两个在开发中经常使用的概念单点登录和消息队列以及具体到中的一些实现方案。单点登录的实质就是安全上下文或凭证在多个应用系统之间的传递或共享。 前言 很久都没有写博客了,这次为大家简单介绍两个在WEB开发中经常使用的概念——单点登录和消息队列以及具体到J2EE中的一些实现方案。本文原创性的工作比较少,主要是一些总结概括和自己的理解。 单点登录SS...

    ARGUS 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<