OAuth2.0实战案例3

简介: 本节介绍如何创建OAuth2授权模块,包括项目搭建、依赖导入、配置文件编写及核心类配置,实现基于Spring Security与OAuth2的认证授权功能,支持数据库存储客户端与令牌信息。

3.创建授权模块

3.1 创建工程并导入依赖

 <artifactId>springboot_security_oauth</artifactId>

 <groupId>com.itheima</groupId>

 <version>1.0-SNAPSHOT</version>

</parent>


<modelVersion>4.0.0</modelVersion>

<artifactId>oauth_server</artifactId>


<dependencies>

 <dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-web</artifactId>

 </dependency>

 <dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-security</artifactId>

 </dependency>

 <dependency>

   <groupId>org.springframework.cloud</groupId>

   <artifactId>spring-cloud-starter-oauth2</artifactId>

 </dependency>

 <dependency>

   <groupId>mysql</groupId>

   <artifactId>mysql-connector-java</artifactId>

   <version>5.1.47</version>

 </dependency>

 <dependency>

   <groupId>org.mybatis.spring.boot</groupId>

   <artifactId>mybatis-spring-boot-starter</artifactId>

   <version>2.1.0</version>

 </dependency>

</dependencies>

3.2 创建配置文件

server:

port: 9001

spring:

datasource:

 driver-class-name: com.mysql.jdbc.Driver

 url: jdbc:mysql:///security_authority

 username: root

 password: root

main:

 allow-bean-definition-overriding: true # 这个表示允许我们覆盖OAuth2放在容器中的bean对象,一定要配置

mybatis:

type-aliases-package: com.itheima.domain

configuration:

 map-underscore-to-camel-case: true

logging:

level:

 com.itheima: debug

3.3 创建启动类

@SpringBootApplication

@MapperScan("com.itheima.mapper")

public class OauthServerApplication {

   public static void main(String[] args) {

       SpringApplication.run(OauthServerApplication.class, args);

   }

}

3.4 创建配置类

@Configuration

@EnableWebSecurity

@EnableGlobalMethodSecurity(securedEnabled = true)

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

 

   @Autowired

   private UserDetailsService myCustomUserService;

   @Bean

   public BCryptPasswordEncoder myPasswordEncoder(){

       return new BCryptPasswordEncoder();

   }

 

   @Override

   protected void configure(HttpSecurity http) throws Exception {

       http.authorizeRequests()

       //所有资源必须授权后访问

       .anyRequest().authenticated()

       .and()

       .formLogin()

       .loginProcessingUrl("/login")

       .permitAll()//指定认证页面可以匿名访问

       //关闭跨站请求防护

       .and().csrf().disable();

   }

 

   @Override

   public void configure(AuthenticationManagerBuilder auth) throws Exception {

       //UserDetailsService类

       auth.userDetailsService(myCustomUserService)

       //加密策略

       .passwordEncoder(myPasswordEncoder());

   }

 

   //AuthenticationManager对象在OAuth2认证服务中要使用,提取放入IOC容器中

   @Override

   @Bean

   public AuthenticationManager authenticationManagerBean() throws Exception {

       return super.authenticationManagerBean();

   }

}

3.5 创建OAuth2授权配置类

@Configuration

@EnableAuthorizationServer

public class OauthServerConfig extends AuthorizationServerConfigurerAdapter {

 

   @Autowired

   private DataSource dataSource;

   @Autowired

   private AuthenticationManager authenticationManager;

   @Autowired

   private UserDetailsService userDetailsService;

 

   //从数据库中查询出客户端信息

   @Bean

   public JdbcClientDetailsService clientDetailsService() {

       return new JdbcClientDetailsService(dataSource);

   }

 

   //token保存策略

   @Bean

   public TokenStore tokenStore() {

       return new JdbcTokenStore(dataSource);

   }

 

   //授权信息保存策略

   @Bean

   public ApprovalStore approvalStore() {

       return new JdbcApprovalStore(dataSource);

   }

 

   //授权码模式专用对象

   @Bean

   public AuthorizationCodeServices authorizationCodeServices() {

       return new JdbcAuthorizationCodeServices(dataSource);

   }

 

   //指定客户端登录信息来源

   @Override

   public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

       clients.withClientDetails(clientDetailsService());

   }

 

   @Override

   public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {

       oauthServer.allowFormAuthenticationForClients();

       oauthServer.checkTokenAccess("isAuthenticated()");

   }

 

   @Override

   public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

       endpoints

       .approvalStore(approvalStore())

       .authenticationManager(authenticationManager)

       .authorizationCodeServices(authorizationCodeServices())

       .tokenStore(tokenStore());

   }

}

相关文章
|
12天前
|
数据采集 人工智能 安全
|
7天前
|
机器学习/深度学习 人工智能 前端开发
构建AI智能体:七十、小树成林,聚沙成塔:随机森林与大模型的协同进化
随机森林是一种基于决策树的集成学习算法,通过构建多棵决策树并结合它们的预测结果来提高准确性和稳定性。其核心思想包括两个随机性:Bootstrap采样(每棵树使用不同的训练子集)和特征随机选择(每棵树分裂时只考虑部分特征)。这种方法能有效处理大规模高维数据,避免过拟合,并评估特征重要性。随机森林的超参数如树的数量、最大深度等可通过网格搜索优化。该算法兼具强大预测能力和工程化优势,是机器学习中的常用基础模型。
344 164
|
6天前
|
机器学习/深度学习 自然语言处理 机器人
阿里云百炼大模型赋能|打造企业级电话智能体与智能呼叫中心完整方案
畅信达基于阿里云百炼大模型推出MVB2000V5智能呼叫中心方案,融合LLM与MRCP+WebSocket技术,实现语音识别率超95%、低延迟交互。通过电话智能体与座席助手协同,自动化处理80%咨询,降本增效显著,适配金融、电商、医疗等多行业场景。
345 155
|
7天前
|
编解码 人工智能 自然语言处理
⚽阿里云百炼通义万相 2.6 视频生成玩法手册
通义万相Wan 2.6是全球首个支持角色扮演的AI视频生成模型,可基于参考视频形象与音色生成多角色合拍、多镜头叙事的15秒长视频,实现声画同步、智能分镜,适用于影视创作、营销展示等场景。
580 4
|
15天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
1018 7