Springboot 实践(9)springboot集成Oauth2.0授权包,5个接口文件配置详解

        前文讲解实现了spring boot集成Oauth2.0,实现了授权服务器和资源服务器的搭建,并通过浏览器和postman测试,获取到了授权码,用携带授权码的URL能够争取范文到资源。

本文详细讲解spring boot集成Oauth2.0的几个重要文件接口,详细如下:

1、授权服务配置接口AuthorizationServerConfigurerAdapter

AuthorizationServerConfigurer接口,其中存在3个方法:

☆ AuthorizationServerSecurityConfigurer:配置令牌端点(Token Endpoint)的安全约束;

☆ ClientDetailsServiceConfigurer:配置OAuth2客户端;

☆ AuthorizationServerEndpointsConfigurer:配置授权(authorization)以及令牌(token)的访问端点和令牌服务(token services);

详细代码如下:bleAuthorizationServer

public class OAuthAuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired

    private DataSource dataSource;

    @Autowired

    private AuthenticationManager authenticationManager;

    @Autowired

    private OAuthWebSecurityConfig oauthWebSecurityConfig;

   

  //AuthorizationServerSecurityConfigurer:配置令牌端点(Token Endpoint)的安全约束;

  //AuthorizationServerSecurityConfigurer继承自SecurityConfigurerAdapter,也就是一个 Spring Security安全配置提供给AuthorizationServer去配置AuthorizationServer的端点(/oauth/****)的安全访问规则、过滤器Filter。

    @Override

    public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception {

        oauthServer

        .tokenKeyAccess("permitAll()")//客户端token调用许可

        .checkTokenAccess("permitAll()")//客户端校验token访问许可

       .allowFormAuthenticationForClients();       

    }

   

    //refresh_token 单独配置UserDetailsService

    @Bean

    public UserDetailsService userDetailsServiceRefresh_token() { 

        return oauthWebSecurityConfig.userDetailsService();

}

    //AuthorizationServerEndpointsConfigurer:配置授权(authorization)以及令牌(token)的访问端点和令牌服务(token services);

    @Override

    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        endpoints // 设置令牌   

                .tokenStore(new JdbcTokenStore(dataSource)).userDetailsService(userDetailsServiceRefresh_token())

                .authenticationManager(authenticationManager);

    }

    @Bean // 声明 ClientDetails实现

    public ClientDetailsService clientDetailsService() {

        return new JdbcClientDetailsService(dataSource);

    }   

    //ClientDetailsServiceConfigurer:配置OAuth2客户端;

    @Override

    public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {

        clients.withClientDetails(clientDetailsService());

    }   

}

2、资源服务配置接口ResourceServerConfigurerAdapter

        ResourceServerConfigurerAdapter (资源服务器配置)内部关联了ResourceServerSecurityConfigurer 和 HttpSecurity。前者与资源安全配置相关,后者与http安全配置相关,详细代码如下:

/*** 资源服务器配置*/

@Configuration

@EnableResourceServer

@EnableWebSecurity

@EnableGlobalMethodSecurity(prePostEnabled = true)

@Order(-1)

public class OAuthResourcesServerConfig extends ResourceServerConfigurerAdapter {

    @Autowired

    private DataSource dataSource;     

    //在每个ResourceServer实例上设置resourceId,该resourceId作为该服务资源的唯一标识。(假如同一个微服务资源部署多份,resourceId相同)

    private static final String DEMO_RESOURCE_ID = "test-resource";

    @Override

    public void configure(ResourceServerSecurityConfigurer resources) {

       resources.resourceId(DEMO_RESOURCE_ID);  //配置resourceServerID

       resources.tokenStore(new JdbcTokenStore(dataSource)); //...... 还可以有有其他的配置 

    }

    @Override

    public void configure(HttpSecurity http) throws Exception {

        http   

            .cors()//开启跨域

            .and()

            .csrf().disable()

            .logout()

            .logoutUrl("/logout")//虚拟的登出地址

            .and()

            .authorizeRequests()

            .antMatchers("/loginSuccess").permitAll()

            .antMatchers("/loginSuccess.html").permitAll()

            .antMatchers("/actuator/health").permitAll()

            .anyRequest().authenticated();

    }

}

3、web安全配置接口WebSecurityConfigurerAdapter

WebSecurityConfigurerAdapter是Spring Security提供了一个抽象类,实现了默认的认证和授权,允许用户自定义一个WebSecurity类,重写其中的三个configure来实现自定义的认证和授权,这三个方法如下:

  • 自定义身份认证的逻辑

protected void configure(AuthenticationManagerBuilder auth) throws Exception { }

  • 自定义全局安全过滤的逻辑

public void configure(WebSecurity web) throws Exception { }

  • 自定义URL访问权限的逻辑

protected void configure(HttpSecurity http) throws Exception { }

详细代码如下:

@Configuration

@EnableWebSecurity

@EnableGlobalMethodSecurity(prePostEnabled = true)

@Order(-1)

public class OAuthWebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Autowired

  private MyUserDetailServiceImpl myUserDetailServiceImpl;

  @Autowired

  private LogoutSuccessHandlerImpl logoutSuccessHandler;

  @Autowired

  private CustomAuthenticationFailureHandler customAuthenticationFailureHandler;

  @Autowired

  private DataSource dataSource;

  /** * @return RememberMe 功能的 Repository */

  @Bean

  public PersistentTokenRepository persistentTokenRepository() {

      // 连接数据库的实现类,还有一个实现类时存内存的InMemoryTokenRepositoryImpl

      JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();

      // 配置数据源

      tokenRepository.setDataSource(dataSource);

      // 在启动数据库时创建存储token的表

      //tokenRepository.setCreateTableOnStartup(true);

      return tokenRepository;

  }

  /******************************remember me***********************************/

  @Override

    @Bean

    public AuthenticationManager authenticationManagerBean() throws Exception {

        return super.authenticationManagerBean();

    }

  /** 放行静态资源 */

  @Override

    public void configure(WebSecurity web) throws Exception {

      web.ignoring().antMatchers("/swagger-ui.html");

      web.ignoring().antMatchers("/swagger-resources/**");

      web.ignoring().antMatchers("/v2/api-docs");

      web.ignoring().antMatchers("/configuration/security");

      web.ignoring().antMatchers("/configuration/ui");

      //web.ignoring().antMatchers("/webjars/springfox-swagger-ui/**");

     

      /************************************************************

       * servlet 3.0 以上的版本支持直接访问 jar 包里面的资源文件。

       * 访问方式:将 jar 包里的 META-INF/resources 目录看成根目录,

       * 则这个目录下的文件都可以直接访问

       * swagger-bootstrap-ui-1.9.6.jar资源放行*/

      web.ignoring().antMatchers("/webjars/**");

      web.ignoring().antMatchers("/doc.html");

     

      web.ignoring().antMatchers("/loginSuccess");

      web.ignoring().antMatchers("/loginSuccess.html");

      web.ignoring().antMatchers("/loginError");

      web.ignoring().antMatchers("/lock");

      web.ignoring().antMatchers("/assets/**");

      //路由跳转允许

      web.ignoring().antMatchers("/api/**");

      web.ignoring().antMatchers("/websocket/**"); //农村饮水安全

      web.ignoring().antMatchers("/Serialnum/EnSure"); //序列号确认

      web.ignoring().antMatchers("/lock"); //锁屏页面

      web.ignoring().antMatchers("/login.html"); //序列号确认

      //放行consul安全监测接口

      web.ignoring().antMatchers("/v1");

      web.ignoring().antMatchers("/actuator/health");

      //web.ignoring().antMatchers("/loginError.html");

      web.ignoring().antMatchers("/favicon.ico");

      //放行单点登录

      web.ignoring().antMatchers("/index");

      web.ignoring().antMatchers("/index/**");

      web.ignoring().antMatchers("/index/oauth_callback");

      //web.ignoring().antMatchers("/myLogout");

     

    }

    @Override

    protected void configure(HttpSecurity http) throws Exception {

        http       

        .requestMatchers().antMatchers(HttpMethod.OPTIONS,"/login", "/oauth/authorize", "/oauth/token")

        .and()

        .cors()//开启跨域

      .and()

      .csrf().disable()              

        .requestMatchers()       

        .antMatchers("/login")

        .antMatchers(HttpMethod.OPTIONS)

        .antMatchers("/oauth/authorize")

        .and()    

      .rememberMe()//记住我相关配置

      .tokenRepository(persistentTokenRepository())

      .tokenValiditySeconds(60*60*10)

        //.antMatchers("/oauth/token")

        .and()           

        .authorizeRequests()

        .anyRequest().authenticated()

        .and()

        .formLogin()

        .loginPage("/login")

           // 自定义登录页面,这里配置了 loginPage, 就会通过 LoginController 的 login 接口加载登录页面

        // 登入成功后,跳转至指定页面

        //.defaultSuccessUrl("/loginSuccess")

        .failureUrl("/loginError").permitAll()       

        .and()

        .logout()

        .logoutUrl("/logout")//虚拟的登出地址

        .logoutSuccessHandler(logoutSuccessHandler);

       

        http.formLogin().failureHandler(customAuthenticationFailureHandler);

    }

   

    @Bean

    @Override

    public UserDetailsService userDetailsService(){

        return myUserDetailServiceImpl;

        //return userAuthenticationProvider;

    }

   

    @Override

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        // 配置用户名密码,这里采用内存方式,生产环境需要从数据库获取   

//        auth.inMemoryAuthentication()

//            .withUser("admin")

//            .password(passwordEncoder().encode("123"))          

//            .roles("USER");

     

        // 使用自定义认证与授权

        auth.userDetailsService(userDetailsService());

    }

    @Bean

    public BCryptPasswordEncoder passwordEncoder(){

        return new BCryptPasswordEncoder();

    }

}

4、用户登录服务接口UserDetailsService

        Spring Security中进行身份验证的是AuthenticationManager接口,ProviderManager是它的一个默认实现,但它并不用来处理身份认证,而是委托给配置好的AuthenticationProvider,每个AuthenticationProvider会轮流检查身份认证。检查后或者返回Authentication对象或者抛出异常。验证身份就是加载响应的UserDetails,看看是否和用户输入的账号、密码、权限等信息匹配。UserDetails中需要重定义loadUserByUsername()函数。

详细代码如下:

@Component

public class MyUserDetailServiceImpl implements UserDetailsService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired

    private SysUserService sysUserService;

    @Autowired

    private SysUserMapper sysUserMapper;

    /**

     * @param username

     * @return

     * @throws UsernameNotFoundException

     */

    @Override

    public UserDetails loadUserByUsername(String username) {

        //1.根据用户名去数据库去查询用户信息获取加密后的密码         

        SysUser sysUser = sysUserMapper.findByName(username);

        if(sysUser==null) {

            logger.info("-------------未查询到用户名:" + username);

            throw new UsernameNotFoundException("Invalid username or password.");

        }else {        

             Boolean stateStr=sysUser.getAccountNonLocked();//锁定状态

            if(stateStr==false){//用户被锁定

                long lockTime = sysUser.getLockTime();

                long sysTime = System.currentTimeMillis();

                long time = sysTime - lockTime;

                if(time>=10*60*1000) { //时间超过10分钟,解锁账户

                    sysUserMapper.updateUNLockedByUserId(username);

                    sysUserMapper.updatelockTimeByUserId(username, 0L);

                    sysUserMapper.updatenLockByUserId(username, 0L);

                    stateStr=true;

                }else {

                    throw new InternalAuthenticationServiceException("该账号已被锁定,请联系管理员!");

                }               

             }

           

            Set<String> permissions = sysUserService.findPermissions(username);        

            List<GrantedAuthority> grantedAuthorities = permissions.stream().map(GrantedAuthorityImpl::new).collect(Collectors.toList());

                        

            return new User(username,

                    //encryptedPassWord,

                    sysUser.getPassword(),

                    true,

                    true,

                    true,

                    stateStr, //锁定状态               

                    grantedAuthorities);

        }         

    } 

}

5、授权失败接口SimpleUrlAuthenticationFailureHandler

        在Spring Security Web框架内部,缺省使用的认证错误处理策略是AuthenticationFailureHandler的实现类SimpleUrlAuthenticationFailureHandler。它由配置指定一个defaultFailureUrl,表示认证失败时缺省使用的重定向地址。一旦认证失败,它的方法onAuthenticationFailure被调用时,它就会将用户重定向到该地址。如果该属性没有设置,它会向客户端返回一个401状态码。另外SimpleUrlAuthenticationFailureHandler还有一个属性useForward,如果该属性设置为true,页面跳转将不再是重定向(redirect)机制,取而代之的是转发(forward)机制。

该处只需要重定义onAuthenticationFailure()详细代码如下:

//3、将登录失败提示到前端展示

@Component

public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

  private Logger logger = LoggerFactory.getLogger(this.getClass());

  private  static ObjectMapper objectMapper = new ObjectMapper();

  @Autowired

  private ThymeleafViewResolver viewResolver;

  private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

   

  @Override

  public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {        

  //此处自行定义授权失败功能。

  }

}

        springboot集成Oauth2.0授权包,接口文件配置详解到此就结束了,学友在实际操作过程中遇到问题,欢迎联系博主。

下文讲解spring cloud的配置与应用,实现服务注册机制。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/51066.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【开个空调】语音识别+红外发射

废话少说&#xff0c;直接上空调板子&#xff1a;YAPOF3。红外接收发射模块用的某宝上发现的YF-33(遗憾解码还没搞清楚&#xff0c;不然做个lirc.conf功能才多)。最后是语音识别用的幻尔的&#xff0c;某宝自然也有&#xff0c;它是个i2c的接口。 本篇胡说八道其实纯粹为了留个…

python 无网络安装库的方法

手动复制库文件&#xff1a; 在你的电脑上找到已安装库的文件&#xff0c;通常位于Python的Lib\site-packages目录下。将这些库文件复制到目标电脑的相同位置。请注意&#xff0c;这种方法仅适用于纯Python库&#xff0c;对于依赖于C扩展或其他系统依赖项的库可能无法正常工作。…

华为云Stack的学习(一)

一、华为云Stack架构 1.HCS 物理分散、逻辑统一、业务驱动、运管协同、业务感知 2.华为云Stack的特点 可靠性 包括整体可靠性、数据可靠性和单一设备可靠性。通过云平台的分布式架构&#xff0c;从整体系统上提高可靠性&#xff0c;降低系统对单设备可靠性的要求。 可用性…

Java后端开发面试题篇——Redis

Redis的数据持久化策略有哪些 RDB的执行原理&#xff1f; bgsave开始时会fork主进程得到子进程&#xff0c;子进程共享主进程的内存数据。完成fork后读取内存数据并写入 RDB 文件。 fork采用的是copy-on-write技术&#xff1a; 当主进程执行读操作时&#xff0c;访问共享内存…

实现外网访问本地服务

最近开发需要其他项目组的人访问我本地服务测试,但又不在同一个地方,不能使用内网访问,所以需要外网访问本地服务功能. 条件: 1.需要一台具备公网IP的服务器 我用的服务器是windows,电脑也是Windows系统 2.下载frp 软件,只需要下载一份就可以了,分别放到服务器上和本地目录既…

无涯教程-PHP - Session选项

从PHP7 起&#xff0c; session_start()()函数接受一系列选项&#xff0c;以覆盖在 php.ini 中设置的会话配置指令。这些选项支持 session.lazy_write &#xff0c;默认情况下此函数为on&#xff0c;如果会话数据已更改&#xff0c;则会导致PHP覆盖任何会话文件。 添加的另一个…

TCP网络连接异常情况的处理

在网络连接中&#xff0c;经常会出现一些意外情况&#xff0c;导致TCP连接不能正常工作&#xff0c;对于这些意外情况&#xff0c;TCP内部有自己的解决方法 一.进程崩溃 在网络通讯时可能会出现进程突然崩溃的情况&#xff0c;当进程崩溃后进程就没了&#xff0c;就会导致进程…

Java实现一个简单的图书管理系统(内有源码)

简介 哈喽哈喽大家好啊&#xff0c;之前作者也是讲了Java不少的知识点了&#xff0c;为了巩固之前的知识点再为了让我们深入Java面向对象这一基本特性&#xff0c;就让我们完成一个图书管理系统的小项目吧。 项目简介&#xff1a;通过管理员和普通用户的两种操作界面&#xff0…

主力吸筹指标及其分析和使用说明

文章目录 主力吸筹指标指标代码分析使用说明使用配图主力吸筹指标 VAR1:=REF(LOW,1); VAR2:=SMA(MAX(LOW-VAR1,0),3,1)/SMA(ABS(LOW-VAR1),3,1)*100; VAR3:=EMA(VAR2,3); VAR4:=LLV(LOW,34); VAR5:=HHV(VAR3,34); VAR7:=EMA(IF(LOW<=VAR4,(VAR3+VAR5*2)/2,0),3); /*底线:0,…

让eslint的错误信息显示在项目界面上

1.需求描述 效果如下 让eslint中的错误&#xff0c;显示在项目界面上 2.问题解决 1.安装 vite-plugin-eslint 插件 npm install vite-plugin-eslint --save-dev2.配置插件 // vite.config.js import { defineConfig } from vite import vue from vitejs/plugin-vue import e…

基于黄金正弦算法优化的BP神经网络(预测应用) - 附代码

基于黄金正弦算法优化的BP神经网络&#xff08;预测应用&#xff09; - 附代码 文章目录 基于黄金正弦算法优化的BP神经网络&#xff08;预测应用&#xff09; - 附代码1.数据介绍2.黄金正弦优化BP神经网络2.1 BP神经网络参数设置2.2 黄金正弦算法应用 4.测试结果&#xff1a;5…

C语言练习1(巩固提升)

C语言练习1 选择题 前言 “人生在勤&#xff0c;勤则不匮。”幸福不会从天降&#xff0c;美好生活靠劳动创造。全面建成小康社会的奋斗目标&#xff0c;为广大劳动群众指明了光明的未来&#xff1b;全面建成小康社会的历史任务&#xff0c;为广大劳动群众赋予了光荣的使命&…

采用typescript编写,实现ofd前端预览、验章

前言 浏览器内核已支持pdf文件的渲染&#xff0c;这极大的方便了pdf文件的阅读和推广。ofd文件作为国产板式标准&#xff0c;急需一套在浏览器中渲染方案。 本人研究ofd多年&#xff0c;分别采用qt、c# 开发了ofd阅读器。本人非前端开发人员&#xff0c;对js、typescript并不熟…

android Bitmap没有recycle()导致native内存暴增

android Bitmap没有recycle()导致native内存暴增 android8.0后&#xff0c;Bitmap从Java层转移到native层&#xff0c;此举虽然缓解了JVM的内存压力&#xff0c;也提升了图形的加载速度&#xff0c;但不恰当的Bitmap分配/释放逻辑&#xff0c;将导致内存问题深藏到native层&am…

windows开发环境搭建

下载msys2&#xff0c;官网下载即可&#xff1a; MSYS2 安装其他的编译工具&#xff08;貌似不需要把中间的命令全部执行&#xff09;&#xff1a; MSYS2使用教程——win10系统64位安装msys2最新版&#xff08;msys2-x86_xxxx.exe&#xff09;_msys64_Dreamhai的博客-CSDN博…

Maven 一键部署到 SSH 服务器

简介 利用 Maven Mojo 功能一键部署 jar 包或 war 包到远程服务器上。 配置 在 maven 的setting.xml 配置服务器 SSH 账号密码。虽然可以在工程的 pom.xml 直接配置&#xff0c;但那样不太安全。 <servers><server><id>iq</id><configuration&…

【C++代码】有序数组的平方,长度最小的子数组,螺旋矩阵 II--代码随想录

题目&#xff1a;有序数组的平方 给你一个按 非递减顺序 排序的整数数组 nums&#xff0c;返回 每个数字的平方 组成的新数组&#xff0c;要求也按 非递减顺序 排序。 题解 数组其实是有序的&#xff0c; 只不过负数平方之后可能成为最大数了。那么数组平方的最大值就在数组的…

食品制造行业云MES系统解决方案

食品饮料行业大致可以分为初级产品加工、二次加工、食品制造、食品分装、调味品和饲料加工等几大类。由于处于产业链不同的位置&#xff0c;其管理存在一定的差异&#xff0c;那么食品行业的MES应该怎么建设呢&#xff1f; 食品饮料行业生产管理特点&#xff1a; 食品饮料行业…

定时任务,Timer,Quartz,Spring Task,Xxl-Job(分布式任务调度框架)

一、定时任务解决方案 1.Thread&#xff0c;Timer&#xff0c;Quartz&#xff0c;Spring Task Thread线程等待&#xff08;最原始最简单方式&#xff09;&#xff0c;创建一个thread&#xff0c;然后让它在while循环里一直运行着&#xff0c;通过sleep方法来达到定时任务的效…

全球Salesforce顾问薪资大揭秘!顾问如何升职加薪?

Salesforce顾问通过针对业务挑战提出、记录和分析需求&#xff0c;提供解决方案&#xff0c;从而帮助企业改善Salesforce的流程和效率。顾问是企业和Salesforce之间的桥梁。 Salesforce顾问的薪资一直是生态系统中的热门话题&#xff0c;备受求职者关注。本篇文章将分享提高顾…