spring oauth2 +springboot sso的案例分析
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇内容主要讲解"spring oauth2 +springboot sso的案例分析",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"spring oaut
千家信息网最后更新 2026年11月03日spring oauth2 +springboot sso的案例分析
本篇内容主要讲解"spring oauth2 +springboot sso的案例分析",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"spring oauth2 +springboot sso的案例分析"吧!
一、依赖
org.springframework.security.oauth.boot spring-security-oauth3-autoconfigure 2.1.6.RELEASE
二、服务端
1.服务端需要的是授权与身份验证,通过配置@EnableAuthorizationServer、@EnableWebSecurity、@EnableResourceServer来完成配置。
2.先来配置WebSecurity的配置
package com.example.oauth;import org.springframework.boot.autoconfigure.security.SecurityProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.annotation.Order;import org.springframework.security.authentication.AuthenticationManager;import org.springframework.security.config.BeanIds;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.builders.WebSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.core.userdetails.User;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.provisioning.InMemoryUserDetailsManager;/** * @author sorata * @date 2019-07-23 09:19 */@Configuration@EnableWebSecurity@Order(SecurityProperties.BASIC_AUTH_ORDER)public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean public UserDetailsService myUserDetailsService(){ return new InMemoryUserDetailsManager(User.builder().username("admin").password(passwordEncoder().encode("admin")).roles("ADMIN").build()); } @Bean public BCryptPasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(myUserDetailsService()).passwordEncoder(passwordEncoder()); } @Bean(BeanIds.AUTHENTICATION_MANAGER) @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override public void configure(WebSecurity web) throws Exception { super.configure(web); } @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable().antMatcher("/**") .authorizeRequests() .antMatchers("/","/login","/oauth/**").permitAll() .anyRequest().authenticated() .and().httpBasic().and().formLogin() .and().logout(); }}note:首先是配置用户UserDetailsService,然后配置密码策略。主要的部分是 configure(HttpSecurity http) 方法,这里当我在formLogin()后配置了验证完成转发,即successForwardUrl("/main")后,sso客户端请求验证完成时,不会跳转到客户端的请求地址,而是跳转到验证成功的服务端地址 /main。
3.编写一个用户信息的controller
package com.example.oauth;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.security.Principal;/** * @author sorata * @date 2019-07-23 09:17 */@RestControllerpublic class UserController { @RequestMapping("/user") public Principal principal(Principal principal){ return principal; } @RequestMapping("/user2") public Principal principal2(Principal principal){ return principal; }}note: 作用就是测试和之后客户端填写的服务器用户信息的url
4.资源服务器
package com.example.oauth;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.oauth3.config.annotation.web.configuration.EnableResourceServer;import org.springframework.security.oauth3.config.annotation.web.configuration.ResourceServerConfigurerAdapter;/** * @author sorata * @date 2019-07-23 09:50 */@Configuration@EnableResourceServerpublic class ResourceConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.csrf().disable().antMatcher("/user") .authorizeRequests().anyRequest().authenticated(); }}5.完成后的效果
6. 重要的认证服务端
package com.example.oauth;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.authentication.AuthenticationManager;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.oauth3.config.annotation.configurers.ClientDetailsServiceConfigurer;import org.springframework.security.oauth3.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;import org.springframework.security.oauth3.config.annotation.web.configuration.EnableAuthorizationServer;import org.springframework.security.oauth3.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;import org.springframework.security.oauth3.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;import org.springframework.security.oauth3.provider.token.TokenStore;import org.springframework.security.oauth3.provider.token.store.InMemoryTokenStore;/** * @author sorata * @date 2019-07-23 10:06 */@Configuration@EnableAuthorizationServerpublic class SsoServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private BCryptPasswordEncoder passwordEncoder; /** * 如果出现错误 在主类上去掉默认配置 * {@link SsoServerApplication} */ @Autowired private UserDetailsService detailsService; @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { security.allowFormAuthenticationForClients() .tokenKeyAccess("permitAll()") .checkTokenAccess("isAuthenticated()") .passwordEncoder(passwordEncoder); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory().withClient("cocos") .secret(passwordEncoder.encode("cocos")) .autoApprove(true) .redirectUris("http://localhost:9090/client/login") .scopes("all") .authorities("ADMIN") .authorizedGrantTypes("authorization_code","password","refresh_token") .accessTokenValiditySeconds(10000) .refreshTokenValiditySeconds(10000); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager) .userDetailsService(detailsService).tokenStore(tokenStore()); } @Bean public TokenStore tokenStore(){ return new InMemoryTokenStore(); }}三、客户端
1.客户端实现比较简单,如果想针对行的修改,自定义那么可以根据后面参考地址研读。
2.application.properties
security.oauth3.client.authentication-scheme=formsecurity.oauth3.client.user-authorization-uri=http://localhost:8080/server/oauth/authorizesecurity.oauth3.client.access-token-uri=http://localhost:8080/server/oauth/tokensecurity.oauth3.client.client-id=cocossecurity.oauth3.client.client-secret=cocossecurity.oauth3.resource.user-info-uri=http://localhost:8080/server/userserver.servlet.context-path=/clientserver.port=9090
3.主类添加注解
package com.example.oauth;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.security.oauth3.client.EnableOAuth3Sso;@SpringBootApplication@EnableOAuth3Ssopublic class SsoClientApplication { public static void main(String[] args) { SpringApplication.run(SsoClientApplication.class, args); }}4.用户的接口
package com.example.oauth;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.security.Principal;/** * @author sorata * @date 2019-07-23 10:30 */@RestControllerpublic class UserController { @RequestMapping("/user") public Principal principal(Principal principal){ return principal; }}四、效果

note: 如果想看到
到此,相信大家对"spring oauth2 +springboot sso的案例分析"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
配置
服务
客户
客户端
用户
验证
案例
案例分析
分析
地址
信息
内容
效果
方法
服务器
学习
实用
更深
重要
成功
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网络科技
广州互联网科技公司
互联网科技作文
互联网科技公司有哪些
上海亿帐通互联网科技
甘肃拓世互联网科技有限公司
互联网之后的科技是什么
中国的互联网科技企业的崛起
团飞科技与伯堂互联网服务平台
想开互联网科技公司怎么样
洛阳共同富裕互联网科技有限公司
深圳艾果互联网通讯科技
互联网数据科技专业
美国互联网科技公司
江苏互联网科技小贷
互联网硬科技公司
云鲸互联网络科技有限公司
互联网科技新闻
同城互联网络科技
山东蛋芒互联网科技有限公司
港股互联网科技股
与互联网科技公司
易互联网科技有限公司
深圳天众互联网科技公司
上游互联网科技
济南小医互联网科技有限公司招聘
互联网科技接地气
悟空问答互联网科技
数字科技和互联网金融哪个好
中国主要互联网科技公司