基于oauth2和spring-security的token认证的实现

基于oauth2和spring-security的token认证的实现

简要说明

  • authenticate: 认证,对用户进行认证
  • authorize: 授权,对认证通过的用户授权,颁发票据,用于访问资源服务器
  • jwt: 后续访问资源服务器的票据(ticket)
  • OAuth2:授权机制的一种,后续实现采用OAuth2中的client credentials方式

实现思路

对于认证相关请求(登录、刷新token)
  • 以auth/form为例,此处不会被OAuth2AuthenticationProcessingFilter拦截
  • http.form配置的认证拦截器为UsernamePasswordAuthenticationFilter
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        //校验相关参数
        if (this.postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        } else {
            String username = this.obtainUsername(request);
            String password = this.obtainPassword(request);
            if (username == null) {
                username = "";
            }

            if (password == null) {
                password = "";
            }

            username = username.trim();
            //封装校验信息
            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
            this
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring-security-oauth2-authorization-server是一个基于Spring Security的OAuth 2.0授权服务器,用于为客户端提供安全的访问资源的授权服务。您可以按照以下步骤使用该库: 1. 在您的Spring Boot项目中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> <version>2.1.0.RELEASE</version> </dependency> ``` 2. 配置您的授权服务器 在您的Spring Boot应用程序中创建一个配置类,并使用@EnableAuthorizationServer注释启用授权服务器。例如: ```java @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { // ... } ``` 3. 配置您的客户端 您可以使用ClientDetailsServiceConfigurer配置您的客户端。例如: ```java @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("client") .secret("{noop}secret") .authorizedGrantTypes("authorization_code", "refresh_token") .scopes("read", "write") .autoApprove(true) .redirectUris("http://localhost:8080/login/oauth2/code/") .and() .withClient("resource") .secret("{noop}secret") .authorizedGrantTypes("client_credentials") .scopes("read"); } ``` 上面的代码将在内存中配置两个客户端:一个用于授权码授权,另一个用于客户端凭证授权。 4. 配置您的用户 您可以使用UserDetailsServiceConfigurer配置您的用户。例如: ```java @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager) .userDetailsService(userDetailsService); } ``` 上面的代码将使用authenticationManager进行身份验证,并使用userDetailsService获取用户信息。 5. 启动您的应用程序 现在,您可以启动您的应用程序,并访问http://localhost:8080/oauth/authorize以获取授权码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值