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

滨州北京网站建设价格廊坊企业做网站

滨州北京网站建设价格,廊坊企业做网站,东莞做网站企业铭,网站怎样制作1. 添加依赖 首先&#xff0c;确保你的项目中包含了Spring Security和OAuth2.0相关的依赖。如果你使用的是Maven&#xff0c;可以在pom.xml中添加以下依赖&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-…

1. 添加依赖

首先,确保你的项目中包含了Spring Security和OAuth2.0相关的依赖。如果你使用的是Maven,可以在pom.xml中添加以下依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency><groupId>org.springframework.security.oauth.boot</groupId><artifactId>spring-security-oauth2-autoconfigure</artifactId><version>2.1.0.RELEASE</version>
</dependency>

运行 HTML

2. 配置OAuth2.0客户端

application.ymlapplication.properties中配置OAuth2.0客户端信息。例如:

spring:security:oauth2:client:registration:google:client-id: your-google-client-idclient-secret: your-google-client-secretscope: email, profile
 

3. 配置Spring Security

创建一个配置类来配置Spring Security,启用OAuth2.0登录功能。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/", "/login**", "/error**").permitAll().anyRequest().authenticated().and().oauth2Login();}
}

4. 创建用户信息服务(可选)

如果你需要自定义用户信息的获取方式,可以实现OAuth2UserService接口。

import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;@Service
public class CustomOAuth2UserService extends DefaultOAuth2UserService {@Overridepublic OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {OAuth2User user = super.loadUser(userRequest);// 在这里可以自定义用户信息的处理逻辑return user;}
}
 

5. 配置OAuth2.0用户信息服务

SecurityConfig中配置自定义的OAuth2UserService

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
import org.springframework.security.oauth2.core.user.OAuth2User;@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate CustomOAuth2UserService customOAuth2UserService;@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/", "/login**", "/error**").permitAll().anyRequest().authenticated().and().oauth2Login().userInfoEndpoint().userService(customOAuth2UserService);}
}
 

6. 运行应用程序

启动应用程序后,访问受保护的资源时,系统会自动重定向到OAuth2.0提供商的登录页面。登录成功后,用户将被重定向回应用程序,并且可以访问受保护的资源。

7. 处理用户信息

在控制器中,你可以通过@AuthenticationPrincipal注解获取当前登录的用户信息。

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@GetMapping("/user")public String user(@AuthenticationPrincipal OAuth2User principal) {return "Hello, " + principal.getAttribute("name");}
}
 

8. 配置资源服务器(可选)

如果你需要保护API资源,可以配置一个资源服务器。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;@Configuration
@EnableWebSecurity
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/api/**").authenticated().and().oauth2ResourceServer().jwt();}@Beanpublic JwtAuthenticationConverter jwtAuthenticationConverter() {JwtGrantedAuthoritiesConverter grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();grantedAuthoritiesConverter.setAuthoritiesClaimName("roles");grantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesConverter);return jwtAuthenticationConverter;}
}

总结

通过以上步骤,你可以在Spring Security中实现OAuth2.0认证与授权。根据具体需求,你可以进一步自定义用户信息的处理、权限控制等。


文章转载自:

http://107GRdWp.qsmch.cn
http://IWAWOtvU.qsmch.cn
http://rwYxTwrr.qsmch.cn
http://EKfXazQH.qsmch.cn
http://kaiCxqsV.qsmch.cn
http://ePGRKeb2.qsmch.cn
http://gW3mpGBY.qsmch.cn
http://V4JIE6bF.qsmch.cn
http://8a9ZXpSX.qsmch.cn
http://45V93Lxj.qsmch.cn
http://rEJGw6Zp.qsmch.cn
http://UvmCqdtx.qsmch.cn
http://SXTsJO87.qsmch.cn
http://g5px2WuN.qsmch.cn
http://7LEtHjhG.qsmch.cn
http://6BCHoA05.qsmch.cn
http://CFJGXsV7.qsmch.cn
http://0TaZxfn7.qsmch.cn
http://J08AdZxN.qsmch.cn
http://7p5aKF2h.qsmch.cn
http://MwE9uHPb.qsmch.cn
http://DLFgz6CD.qsmch.cn
http://SI2NiFKz.qsmch.cn
http://PELbNPwQ.qsmch.cn
http://lUamqsI8.qsmch.cn
http://ppBiXJKC.qsmch.cn
http://p5WCtJMg.qsmch.cn
http://UNn35Qkb.qsmch.cn
http://8npGsoFf.qsmch.cn
http://XzN0OXgZ.qsmch.cn
http://www.dtcms.com/wzjs/641796.html

相关文章:

  • 政务网站建设原则营销型网站建站
  • 宁波网站推广找哪家国外高大上设计网站
  • 怎么样黑进网站后台网站开发工具哪个好
  • 网站开发项目团队wp博客seo插件
  • 网站免费响应建设手机上的编程软件
  • 厦门 外贸商城网站国际网站 建设
  • 专业建设润滑油网站如何开通网络
  • 网站常用布局设计制作小车二教学视频
  • 内蒙古建设工程造价信息网官方网站还是网站好
  • 网站关键词 价格生成设计之家效果图
  • 东明网站建设wordpress 首页模板
  • 网站不能调用样式加人引流加人网站怎么做
  • 中信云 做网站如何设置个人网站
  • 重庆自适应网站建设做网站开发钱
  • 网站建设与管理案例教程第三版答案深圳网站设计 深圳信科
  • 镇江专业网站建设制作wordpress建群站
  • 做文字头像的网站网站与微信结合
  • 网站发布初期的推广黄金网站软件app大全
  • 北京首华建设经营有限公司网站网站标题能改吗
  • 楼盘网站建设案例编程课网课哪家好
  • 江门网站推广多少钱找段子的各大网站
  • 安卓系统上怎样做网站前端开发萤栈WordPress
  • 网站精品案例南昌网站开发技术
  • 南京seo按天计费seo属于运营还是技术
  • 同一个空间可以做两个网站么六安网站建设找哪家
  • 北京做网站的公司排名做直播网站软件有哪些软件
  • 郑州网站免费制作娃哈哈网络营销策划方案
  • 内蒙古自治区建设厅网站婚庆公司网站建设方案
  • 网站外链暴涨淄博周村网站建设报价
  • 做网站什么最重要有哪些公众号是小黄油的