springboot整合oauth2实现token认证

炫舞风中      2022-05-17     454

关键词:

参考地址:https://www.jianshu.com/p/19059060036b

session和token的区别:

  • session是空间换时间,而token是时间换空间。session占用空间,但是可以管理过期时间,token管理部了过期时间,但是不占用空间.
  • sessionId失效问题和token内包含。
  • session基于cookie,app请求并没有cookie 。
  • token更加安全(每次请求都需要带上)

Oauth2 密码授权流程

在oauth2协议里,每一个应用都有自己的一个clientId和clientSecret(需要去认证方申请),所以一旦想通过认证,必须要有认证方下发的clientId和secret。

1. pom

       <!--security-->
        <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>

2. UserDetail实现认证第一步

MyUserDetailsService.java

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 根据进行登录
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("登录用户名:"+username);
        String password = passwordEncoder.encode("123456");
        //User三个参数   (用户名+密码+权限)
        //根据查找到的用户信息判断用户是否被冻结
        log.info("数据库密码:"+password);
        return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }

3. 获取token的控制器

 1 @RestController
 2 public class OauthController {
 3 
 4     @Autowired
 5     private ClientDetailsService clientDetailsService;
 6     @Autowired
 7     private AuthorizationServerTokenServices authorizationServerTokenServices;
 8     @Autowired
 9     private AuthenticationManager authenticationManager;
10 
11     @PostMapping("/oauth/getToken")
12     public Object getToken(@RequestParam String username, @RequestParam String password, HttpServletRequest request) throws IOException {
13         Map<String,Object>map = new HashMap<>(8);
14         //进行验证
15         String header = request.getHeader("Authorization");
16         if (header == null && !header.startsWith("Basic")) {
17             map.put("code",500);
18             map.put("message","请求投中无client信息");
19             return map;
20         }
21         String[] tokens = this.extractAndDecodeHeader(header, request);
22         assert tokens.length == 2;
23         //获取clientId 和 clientSecret
24         String clientId = tokens[0];
25         String clientSecret = tokens[1];
26         //获取 ClientDetails
27         ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
28         if (clientDetails == null){
29             map.put("code",500);
30             map.put("message","clientId 不存在"+clientId);
31             return map;
32             //判断  方言  是否一致
33         }else if (!StringUtils.equals(clientDetails.getClientSecret(),clientSecret)){
34             map.put("code",500);
35             map.put("message","clientSecret 不匹配"+clientId);
36             return map;
37         }
38         //使用username、密码进行登录
39         UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(username, password);
40         //调用指定的UserDetailsService,进行用户名密码验证
41         Authentication authenticate = authenticationManager.authenticate(authentication);
42         HrUtils.setCurrentUser(authenticate);
43         //放到session中
44         //密码授权 模式, 组建 authentication
45         TokenRequest tokenRequest = new TokenRequest(new HashMap<>(),clientId,clientDetails.getScope(),"password");
46 
47         OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
48         OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request,authentication);
49 
50         OAuth2AccessToken token = authorizationServerTokenServices.createAccessToken(oAuth2Authentication);
51         map.put("code",200);
52         map.put("token",token.getValue());
53         map.put("refreshToken",token.getRefreshToken());
54         return map;
55     }
56 
57     /**
58      * 解码请求头
59      */
60     private String[] extractAndDecodeHeader(String header, HttpServletRequest request) throws IOException {
61         byte[] base64Token = header.substring(6).getBytes("UTF-8");
62 
63         byte[] decoded;
64         try {
65             decoded = Base64.decode(base64Token);
66         } catch (IllegalArgumentException var7) {
67             throw new BadCredentialsException("Failed to decode basic authentication token");
68         }
69 
70         String token = new String(decoded, "UTF-8");
71         int delim = token.indexOf(":");
72         if (delim == -1) {
73             throw new BadCredentialsException("Invalid basic authentication token");
74         } else {
75             return new String[]{token.substring(0, delim), token.substring(delim + 1)};
76         }
77     }
78 }
View Code

4. 核心配置

(1)、Security 配置类 说明登录方式、登录页面、哪个url需要认证、注入登录失败/成功过滤器

 1 @Configuration
 2 public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {
 3 
 4     /**
 5      * 注入 自定义的  登录成功处理类
 6      */
 7     @Autowired
 8     private MyAuthenticationSuccessHandler mySuccessHandler;
 9     /**
10      * 注入 自定义的  登录失败处理类
11      */
12     @Autowired
13     private MyAuthenticationFailHandler myFailHandler;
14 
15     @Autowired
16     private ValidateCodeFilter validateCodeFilter;
17 
18     /**
19      * 重写PasswordEncoder  接口中的方法,实例化加密策略
20      * @return 返回 BCrypt 加密策略
21      */
22     @Bean
23     public PasswordEncoder passwordEncoder(){
24         return new BCryptPasswordEncoder();
25     }
26 
27     @Override
28     protected void configure(HttpSecurity http) throws Exception {
29         //在UsernamePasswordAuthenticationFilter 过滤器前 加一个过滤器 来搞验证码
30         http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
31                 //表单登录 方式
32                 .formLogin()
33                 .loginPage("/authentication/require")
34                 //登录需要经过的url请求
35                 .loginProcessingUrl("/authentication/form")
36                 .passwordParameter("pwd")
37                 .usernameParameter("user")
38                 .successHandler(mySuccessHandler)
39                 .failureHandler(myFailHandler)
40                 .and()
41                 //请求授权
42                 .authorizeRequests()
43                 //不需要权限认证的url
44                 .antMatchers("/oauth/*","/authentication/*","/code/image").permitAll()
45                 //任何请求
46                 .anyRequest()
47                 //需要身份认证
48                 .authenticated()
49                 .and()
50                 //关闭跨站请求防护
51                 .csrf().disable();
52         //默认注销地址:/logout
53         http.logout().
54                 //注销之后 跳转的页面
55                 logoutSuccessUrl("/authentication/require");
56     }
57 
58     /**
59      * 认证管理
60      *
61      * @return 认证管理对象
62      * @throws Exception 认证异常信息
63      */
64     @Override
65     @Bean
66     public AuthenticationManager authenticationManagerBean() throws Exception {
67         return super.authenticationManagerBean();
68     }
69 }
View Code

(2)、认证服务器

 1 @Configuration
 2 @EnableAuthorizationServer
 3 public class MyAuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
 4     @Autowired
 5     private AuthenticationManager authenticationManager;
 6 
 7     @Autowired
 8     private MyUserDetailsService userDetailsService;
 9 
10 
11 
12 
13     @Override
14     public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
15         super.configure(security);
16     }
17 
18     /**
19      * 客户端配置(给谁发令牌)
20      * @param clients
21      * @throws Exception
22      */
23     @Override
24     public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
25         clients.inMemory().withClient("internet_plus")
26                 .secret("internet_plus")
27                 //有效时间 2小时
28                 .accessTokenValiditySeconds(72000)
29                 //密码授权模式和刷新令牌
30                 .authorizedGrantTypes("refresh_token","password")
31                 .scopes( "all");
32     }
33 
34     @Override
35     public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
36         endpoints
37                 .authenticationManager(authenticationManager)
38                 .userDetailsService(userDetailsService);
39     }
40 }
View Code

@EnableResourceServer这个注解就决定了这是个资源服务器。它决定了哪些资源需要什么样的权限。

5、测试

springboot整合oauth2(代码片段)

springboot整合oauth2.0文章目录springboot整合oauth2.01.概念2.springboot整合oauth2.0示例2.1SecurityConfig配置2.2资源服务器配置和授权服务器配置3.测试环境springboot1.5.xdemo下载:https://github.com/2010yhh/springBoot-demos/tree/master/springb 查看详情

(十三)整合springcloud云架构-sso单点登录之oauth2.0根据token获

上一篇我根据框架中OAuth2.0的使用总结,画了SSO单点登录之OAuth2.0登出流程,今天我们看一下根据用户token获取yoghurt信息的流程:/***根据token获取用户信息*@paramaccessToken*@return*@throwsException*/@RequestMapping(value="/user/token/{accesstoken}",met... 查看详情

springboot整合jwt和请求拦截,实现利用token做请求安全拦截校验,且实现阻止并发登录(代码片段)

目录一、导入依赖二、编写jwt工具类,实现生成token和解析token三、在登录请求中向redis中添加token信息1、先注入redis的接口类2、在登录方法中生成token并插入redis,有效期一天四、实现请求拦截器1、编写自定义的请求拦截... 查看详情

5分钟springboot极速整合jwt生成token,一篇文章带你快速了解原理并使用(代码片段)

SpringBoot整合jwt登录功能的实现实现原理Session、Cookie、Token的使用状况实践测试生成Token给客户端pojoJwtUtilController解决跨域问题Vue验证客户端存储Tokenrouter.jsError.vueJwtUtil.javaUserController.java运行效果专业解读登录功能的实现登陆页面... 查看详情

使用 SpringBoot 2 自动化 OAuth2 refresh_token 过程

】使用SpringBoot2自动化OAuth2refresh_token过程【英文标题】:automatetheOAuth2refresh_tokenprocesswithSpringBoot2【发布时间】:2022-01-0203:56:16【问题描述】:我有一个SpringBoot2应用程序,一个作为resource-server的MainApp,作为AuthorizationServer的KeyCloa... 查看详情

springboot/angular整合keycloak实现单点登录

未完待续KeycloakKeycloak为现代应用和服务提供开源的认证和访问管理,即通常所说的认证和授权。Keycloak支持OpenID、OAuth2.0和SAML2.0协议;支持用户注册、用户管理、权限管理;支持代理OpenID、SAML2.0IDP,支持GitHub、LinkedIn等第三方登... 查看详情

springboot1springboot整合jwt实现token验证(代码片段)

这里写目录标题1.单点登录1.1单系统登录1.1.1单系统登录流程(使用Session实现单系统登录)1.2多系统(单点)登录1.单点登录单点登录(SingleSignOn),简称为SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义:在多个应用系统中,用... 查看详情

springboot+shiro框架整合实现前后端分离的权限管理基础demo

记录一下使用SpringBoot集成Shiro框架实现前后端分离Web项目的过程,后端使用SpringBoot整合Shiro,前端使用vue+elementUI,达到前后端使用token来进行交互的应用,这种方式通常叫做无状态,后端只需要使用Shiro框... 查看详情

springboot集成fastdfs+nginx整合基于token的防盗链

  为什么要用SpringBoot?  SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方... 查看详情

Spring Boot 2 + OAuth2:为 Token 配置 Auth Code 交换

】SpringBoot2+OAuth2:为Token配置AuthCode交换【英文标题】:SpringBoot2+OAuth2:ConfigureExchangeofAuthCodeforToken【发布时间】:2018-08-2502:16:56【问题描述】:我已经按照SpringBootOAuth2教程来配置OAuth2客户端。不幸的是,一旦“用户”通过Idp(Okta)... 查看详情

springsecurity注解鉴权(整合springboot,jwt,redis)

参考技术A该类实现了UserDetails接口内含登录成功或失败后的处理方法用一个简单的类来接受前端出来的登录信息实现UserDetailsService接口,重写loadUserByUsername方法,按自己的实际需求来编写验证规则该类为token校验器,并封装了用... 查看详情

springboot整合jwt

    这两天写了写登录的逻辑,从刚开始用cookie,到用jwttoken,到单点登录,随着系统的不断升级,所用技术也在随之改变升级. 其中也遇到了一些坑.jwt令牌一直想着使用无状态,后来发现并不能完全无状态,无状态只是理想状态.... 查看详情

全网最细致springboot整合springsecurity+jwt实现用户认证(代码片段)

【全网最细致】SpringBoot整合SpringSecurity+JWT实现用户认证  登录和用户认证是一个网站最基本的功能,在这篇博客里,将介绍如何用SpringBoot整合SpringSecurity+JWT实现登录及用户认证文章目录【全网最细致】SpringBoot整... 查看详情

oauth2生成token代码备忘

参考技术A调用tokenEndpoint.postAccessToken生成token时,接口调用逻辑:1、调用AuthenticationProvider接口(AdminAuthenticationProvider实现类)密码校验2、调用UserDetailsService接口(MyUserDetailsService实现类)获取用户信息3、调用DefaultTokenServices接... 查看详情

satoken使用springboot整合satokentoken自动续期+token定期刷新+注解鉴权(代码片段)

目录🥤一、需求1、token自动续期2、token定期刷新3、注解鉴权🏺二、项目搭建1、引入依赖2、配置文件3、全局配置4、全局异常处理5、自定义拦截器(token续期和定期刷新)6、自定义权限验证接口🥃三、注解... 查看详情

Spring boot security oauth2 从 cookie 获取 access_token

】Springbootsecurityoauth2从cookie获取access_token【英文标题】:Springbootsecurityoauth2getaccess_tokenfromcookie【发布时间】:2018-07-0419:43:48【问题描述】:我目前正在几个SpringBoot应用程序之间实现身份验证。目前,jwt访问令牌在授权标头中发... 查看详情

java安全框架-springboot整合jwt

文章目录1.JWT案列使用1.1.pom.xml导入依赖1.2.生成token1.3.验证token1.4.常见的异常2.Springboot整合JWT2.1.封装JWT工具类2.2.导入依赖2.3.appliaction.properties2.4.数据库配置2.5.Dao层2.6.UserDaoMapper.xml2.7.Service层2.8.Controller层2.9使用拦截器进行优化2 查看详情

oauth2整合security(代码片段)

什么是oauth2OAuth2.0是一个授权协议,它允许软件应用代表(而不是充当)资源拥有者去访问资源拥有者的资源。应用向资源拥有者请求授权,然后取得令牌(token),并用它来访问资源,并且资源拥... 查看详情