SpringBoot 整合 oauth2实现 token 认证

参考地址: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 }

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 }

(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 }

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

5、测试

原文地址:https://www.cnblogs.com/cq-yangzhou/p/12159964.html

时间: 2024-08-04 02:29:19

SpringBoot 整合 oauth2实现 token 认证的相关文章

springboot整合shiro实现登录认证以及授权

1.添加shiro的依赖 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-web- starter</artifactId> <version>1.4.0</version> </dependency> 2.先创建一个Realm public class MyShiroRealm extends Aut

SpringBoot(十四):springboot整合shiro-登录认证和权限管理

原文出处: 纯洁的微笑 这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在Java领域一般有Spring Security.Apache Shiro等安全框架,但是由于Spring Security过于庞大和复杂,大多数公司会选择Apache Shiro来使用,这篇文章会先介绍一下Apache Shiro,在结合Spring Boot给出使用案例. Apache Shiro What is Ap

QQ登录整合/oauth2.0认证-02-跳转到QQ互联页

---------------------------目录---------------------------------- QQ登录整合/oauth2.0认证-01-申请appkey和appid ---------------------------正文---------------------------------- 在上一讲里面,主要是 要读者们 先准备一个 appid 和appkey,准备这个 可能需要一段时间审核,公司类型的可能稍微慢一点 其实 关键是 你能有一个可以绑定上域名的空

QQ登录整合/oauth2.0认证-04-调整到QQ互联进行QQ登录

---------------------------------目录------------------------------------- QQ登录整合/oauth2.0认证-03-对第二节的代码改进 (2015-07-07 16:10) QQ登录整合/oauth2.0认证-02-跳转到QQ互联页 (2015-07-06 20:25) QQ登录整合/oauth2.0认证-01-申请appkey和appid (2015-07-06 20:05) -----------------------

SpringBoot系列十二:SpringBoot整合 Shiro

1.概念:SpringBoot 整合 Shiro 2.具体内容 Shiro 是现在最为流行的权限认证开发框架,与它起名的只有最初的 SpringSecurity(这个开发框架非常不好用,但是千万不要 以为 SpringSecurity 没有用处,它在 SpringCloud 阶段将发挥重大的作用).但是现在如果要想整合 Shiro 开发框架有一点很遗憾, SpringBoot 没有直接的配置支持,它不像整合所谓的 Kafka.Redis.DataSource,也就是说如果要想整合 Shiro 开

SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统

1.前言本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelper.Mapper插件.druid.dataTables.ztree.jQuery 开发工具:intellij idea 数据库:mysql.redis 2.表结构还是是用标准的5张表来展现权限.如下图:image 分别为用户表,角色表,资源表,用户角色表,角色资源表.在这个demo中使用了mybat

SpringBoot整合Shiro 集成Redis缓存(六)

简介:由于考虑到项目后期分布式部署,所以缓存由ehcache改为redis,而redis既有单机版部署,也有分布式部署,所以二者需要兼容. 1. maven依赖 <dependency> <groupId>org.crazycake</groupId> <artifactId>shiro-redis</artifactId> <version>3.1.0</version> </dependency> 2. 设

基于Spring oauth2.0统一认证登录,返回自定义用户信息

先看源码是如何处理的:   package org.springframework.boot.autoconfigure.security.oauth2.resource; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.logging.Log

SpringSecurity解决跨域问题,在SpringBoot整合SprinSecurity中如何用前后端分离Ajax登录,Ajax登录返回状态200还是近error

先说说SpringSecurity如何实现前后端分离Ajax登录? 今天使用SpringBoot整合SpringSecurity中想使用Ajax替代SpringSecurit的Form表单提交,在这里我们的提交方式还是使用表单提交 http.formLogin().loginProcessingUrl("/authentication/form") loginProcessingUrl方法表示你登录请求的地址,在这里SpringSecurity默认登录页面地址是/login ,填写了u