当前位置: 首页 > wzjs >正文

网站栏目管理建设建设摩托官方网站u爽

网站栏目管理建设,建设摩托官方网站u爽,旅游类网站如何做推广,工程建设工程信息网在 Spring Security 中集成 OAuth 2.0 可以实现安全的第三方认证和资源保护。以下是完整的配置指南和代码示例: 一、OAuth 2.0 核心概念 角色作用资源所有者用户(授权访问资源的人)客户端应用(如Web、移动端)授权服务…

在 Spring Security 中集成 OAuth 2.0 可以实现安全的第三方认证和资源保护。以下是完整的配置指南和代码示例:


一、OAuth 2.0 核心概念

角色作用
资源所有者用户(授权访问资源的人)
客户端应用(如Web、移动端)
授权服务器颁发访问令牌(如 Google、自建服务器)
资源服务器存储受保护资源,验证令牌后允许访问(如API服务)

二、快速实现 OAuth 2.0 登录(第三方登录)

1. 添加依赖
<!-- Spring Security OAuth2 Client -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
2. 配置第三方 OAuth 2.0 客户端(以 Google 为例)
# application.yml
spring:security:oauth2:client:registration:google:client-id: your-google-client-idclient-secret: your-google-client-secretscope: email, profile # 请求的权限范围
3. 启用 OAuth 2.0 登录
@Configuration
@EnableWebSecurity
public class SecurityConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated() // 所有请求需认证.and().oauth2Login() // 启用 OAuth2 登录.userInfoEndpoint().userService(customOAuth2UserService); // 自定义用户信息处理return http.build();}@Autowiredprivate CustomOAuth2UserService customOAuth2UserService;
}
4. 自定义用户信息处理
@Service
public class CustomOAuth2UserService extends DefaultOAuth2UserService {@Overridepublic OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {OAuth2User user = super.loadUser(userRequest);// 提取用户信息并转换为应用内的用户模型return new CustomOAuth2User(user);}
}

三、自建 OAuth 2.0 授权服务器

1. 添加依赖
<!-- 授权服务器支持 -->
<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-oauth2-authorization-server</artifactId><version>0.4.0</version> <!-- 检查最新版本 -->
</dependency>
2. 配置授权服务器
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {@Autowiredprivate PasswordEncoder passwordEncoder;@Autowiredprivate AuthenticationManager authenticationManager;@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient("client-id").secret(passwordEncoder.encode("client-secret")).authorizedGrantTypes("authorization_code", "refresh_token").scopes("read", "write").redirectUris("http://localhost:8080/login/oauth2/code/custom");}@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) {endpoints.authenticationManager(authenticationManager);}
}

四、资源服务器配置(保护API)

1. 添加依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
2. 配置资源服务器
@Configuration
@EnableWebSecurity
public class ResourceServerConfig {@Beanpublic SecurityFilterChain filterChain(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/api/**").authenticated().and().oauth2ResourceServer().jwt(); // 使用 JWT 令牌验证return http.build();}// JWT 解码配置@Beanpublic JwtDecoder jwtDecoder() {return NimbusJwtDecoder.withJwkSetUri("http://auth-server/.well-known/jwks.json").build();}
}

五、OAuth 2.0 授权模式

1. 授权码模式(最安全)
GET /oauth2/authorize?response_type=code&client_id=client-id&redirect_uri=http://client/callback&scope=read
2. 密码模式(仅信任客户端)
// 配置客户端支持密码模式
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient("client-id").secret(passwordEncoder.encode("secret")).authorizedGrantTypes("password", "refresh_token").scopes("read");
}

六、JWT 令牌配置

1. 生成 JWT 令牌
@Bean
public JwtEncoder jwtEncoder() {return new NimbusJwtEncoder(new ImmutableSecret<>(secretKey.getBytes()));
}@Bean
public JwtGenerator jwtGenerator(JwtEncoder jwtEncoder) {return new JwtGenerator(jwtEncoder);
}
2. 自定义令牌声明
public class CustomJwtTokenEnhancer implements JwtEncoder {@Overridepublic Jwt encode(JwtEncoderParameters parameters) throws JwtEncodingException {JwsHeader headers = JwsHeader.with(MACAlgorithm.HS256).build();JwtClaimsSet claims = parameters.getClaims().build();// 添加自定义声明claims.claim("user_role", "ADMIN");return new Jwt(headers, claims, "signature");}
}

七、安全最佳实践

  1. 强制 HTTPS
    生产环境必须启用 HTTPS,防止令牌泄露。

    http.requiresChannel().anyRequest().requiresSecure();
    
  2. 令牌存储安全
    避免在客户端存储令牌,推荐使用 HttpOnly Cookie。

  3. 密钥管理
    使用加密的密钥库(如 Vault)存储 client-secret 和 JWT 签名密钥。

  4. 令牌有效期
    设置短期的访问令牌和可刷新的长期令牌:

    .accessTokenValiditySeconds(3600) // 1小时
    .refreshTokenValiditySeconds(2592000); // 30天
    

八、常见问题解决

1. 令牌无效或过期

检查项
• 令牌签名是否正确
• 有效期是否过期
• 令牌是否被撤销

2. 跨域问题

确保授权服务器的 CORS 配置允许客户端域:

@Bean
public CorsConfigurationSource corsConfigurationSource() {CorsConfiguration config = new CorsConfiguration();config.setAllowedOrigins(Arrays.asList("http://client-domain"));config.setAllowedMethods(Arrays.asList("GET", "POST"));UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", config);return source;
}

总结

通过 Spring Security 集成 OAuth 2.0,您可以实现以下功能:
第三方登录(如 Google、GitHub)
安全的 API 保护(JWT 或 Opaque 令牌)
自建授权服务器(支持多客户端、多授权模式)

关键配置点:
oauth2Client 配置第三方登录
@EnableAuthorizationServer 创建授权服务器
oauth2ResourceServer 保护资源
• 结合 JWT 实现无状态认证

遵循安全最佳实践,确保令牌安全和系统防护。

http://www.dtcms.com/wzjs/561598.html

相关文章:

  • 做网站要学的东西桂市做网站的朋友
  • 网站兼容手机浏览器传奇游戏排行榜前十名
  • 建设银行网站查询不显示整存争取金额申请网站空间
  • 关于网站建设意见和建议凡科网站怎么样
  • 长安网站建设方案做印量调查的网站
  • 建网站用什么服务器好3d建模师
  • 深圳做网站公司哪家好梅河口市建设局网站
  • 网站搭建设计自己电脑可以做网站服务器
  • 网站流量好难做腾讯云网站安全认证
  • 台州快速建站公司桂林网站建
  • 红酒手机网站建设wordpress文章不能写入关键词
  • 网站网页制作模板企业宣传片拍摄思路
  • 海外seo网站推广style图片路径wordpress
  • 网络营销导向企业网站建设的原则包括专业建筑工程网站
  • 大城县企业网站建设建设网站建设哪家快
  • 个人网站设计步骤做环保是跑还是网站卖
  • 佛山中小企业网站建设重庆企业网站定制
  • 德国和俄罗斯和做视频网站政务网站建设及安全
  • 门户网站 架构怎么加php网站登陆源码
  • 用别的公司域名做网站如何用源代码提取网页的图片
  • 泰安网站制作工作室品牌搜索引擎服务优化
  • 软件下载网站制作如何建网站赚钱
  • 手机怎么打开自己做的网站昆明有几个区
  • 网站设计与开发公司wordpress盒子
  • 企业网站建设的参考文献百度官网进入
  • 我和你99谁做的网站什么是网络营销最重要的工具
  • 哈尔滨网络科技公司做网站学校门户网站建设的好处
  • 局网站建设情况网站建设案例基本流程
  • 安徽p2p网站建设一家企业如何建设自己的网站 下载
  • 动态手机网站常州个性化网站建设