Last time, we integrated verification code login and applet login into Spring Security elegantly, and many students were hooked. Compared with some traditional gameplay, it was much more advanced. The fat brother quickly seized the opportunity to draw inferences from other facts, and also connected several non-standard OAuth2, mainly WeChat and enterprise WeChat, so as to do everything possible.
The integration can be completed with the following simple lines of code:
@Bean
DelegateClientRegistrationRepository delegateClientRegistrationRepository(@Autowired(required = false) OAuth2ClientProperties properties) {
DelegateClientRegistrationRepository clientRegistrationRepository = new DelegateClientRegistrationRepository();
if (properties != null) {
List<ClientRegistration> registrations = new ArrayList<>(
OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties).values());
registrations.forEach(clientRegistrationRepository::addClientRegistration);
}
return clientRegistrationRepository;
}
This is to be compatible with the OAuth2 client configuration in the application.yaml
configuration file, the default WeChat configuration and other well-known third-party configurations, you can also use the DelegateClientRegistrationRepository
setDelegate
method to Extend the way to get client configuration:
public void setDelegate(Function<String, ClientRegistration> delegate) {
this.delegate = delegate;
}
Then in HttpSecurity
your configuration is completely OK:
httpSecurity.apply(new OAuth2ProviderConfigurer(delegateClientRegistrationRepository))
// 微信网页授权 下面的参数是假的
.wechatWebclient("wxdf90xxx8e7f", "bf1306baaaxxxxx15eb02d68df5")
// 企业微信登录 下面的参数是假的
.workWechatWebLoginclient("wwa70dc5b6e56936e1",
"nvzGI4Alp3xxxxxxZUc3TtPtKbnfTEets5W8", "1000005")
// 微信扫码登录 下面的参数是假的
.wechatWebLoginclient("xxxxxxxx", "xxxxxxxx")
.oAuth2LoginConfigurerConsumer(oauth2Configurer->
oauth2Configurer.successHandler(new ForwardAuthenticationSuccessHandler("/"))
);
Just configure the account and you are done. It is not simple, and the scalability is still guaranteed, which can fully meet your personalized needs. If you want the database to manage these parameters, you can extend it yourself, it is not difficult.
The effect of logging in is as follows:
A little change to a custom page, isn't it taller?
After successful login, you can write a /
interface:
@GetMapping("/")
public Map<String, Object> index(@RegisteredOAuth2AuthorizedClient
OAuth2AuthorizedClient oAuth2AuthorizedClient) {
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
Map<String, Object> map = new HashMap<>(2);
// OAuth2AuthorizedClient 为敏感信息不应该返回前端
map.put("oAuth2AuthorizedClient", oAuth2AuthorizedClient);
map.put("authentication", authentication);
// todo 处理登录注册的逻辑 处理权限问题
// todo 根据 authentication 生成 token cookie之类的
// todo 也可以用 AuthenticationSuccessHandler 配置来替代
return map;
}
According to the Authentication
information to return the token or cookie
, it can be achieved. You can also configure a AuthenticationSuccessHandler
without writing an interface.
If you have other third-party OAuth2 to connect, you can provide it to Fat Brother, and Fat Brother will help you to do it for free.
The project and DEMO address are: https://gitee.com/felord/spring-security-login-extension Remember to give a star!
关注公众号:Felordcn 获取更多资讯
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。