OauthDemo
2022/7/25 6:52:59
本文主要是介绍OauthDemo,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
AuthorizationServerConfiguration.java
@Configuration @EnableAuthorizationServer public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private JwtAccessTokenConverter jwtAccessTokenConverter; @Autowired private ClientDetailsService jdbcClientDetailsService; @Autowired private AuthorizationCodeServices authorizationCodeServices; @Autowired private ApprovalStore approvalStore; @Autowired private TokenStore tokenStore; @Autowired private OauthTokenEnhancer oauthTokenEnhancer; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) { endpoints.authenticationManager(authenticationManager); endpoints.accessTokenConverter(jwtAccessTokenConverter); endpoints.authorizationCodeServices(authorizationCodeServices); endpoints.approvalStore(approvalStore); endpoints.tokenStore(tokenStore); TokenEnhancerChain enhancerChain = new TokenEnhancerChain(); List<TokenEnhancer> delegates = new ArrayList<>(); delegates.add(oauthTokenEnhancer); delegates.add(jwtAccessTokenConverter); enhancerChain.setTokenEnhancers(delegates); endpoints.tokenEnhancer(enhancerChain); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.withClientDetails(jdbcClientDetailsService); } @Override public void configure(AuthorizationServerSecurityConfigurer security) { security.allowFormAuthenticationForClients() .tokenKeyAccess("permitAll()") .checkTokenAccess("permitAll()"); } }
JwtTokenConfiguration.java
@Configuration public class JwtTokenConfiguration { @Bean public JwtAccessTokenConverter jwtAccessTokenConverter() { return new JwtAccessTokenConverter(); } }
WebSecurityConfiguration.java
@Configuration @EnableWebSecurity public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired private DataSource dataSource; @Autowired private UserAuthService userAuthService; @Bean public BCryptPasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean public AuthorizationCodeServices authorizationCodeServices() { return new JdbcAuthorizationCodeServices(dataSource); } @Bean public ApprovalStore approvalStore() { return new JdbcApprovalStore(dataSource); } @Bean public TokenStore tokenStore() { return new JdbcTokenStore(dataSource); } @Bean public OauthTokenEnhancer oauthTokenEnhancer() { return new OauthTokenEnhancer(); } @Bean public ClientDetailsService jdbcClientDetailsService() { return new JdbcClientDetailsService(dataSource); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override protected void configure(AuthenticationManagerBuilder auth) { DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); authProvider.setPasswordEncoder(passwordEncoder()); authProvider.setUserDetailsService(userAuthService); auth.authenticationProvider(authProvider); } }
OauthController.java
@RestController @RequestMapping("/oauth") public class OauthController { @Autowired private TokenEndpoint tokenEndpoint; @GetMapping("/token") public Oauth getAccessToken(Principal principal, @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException { return tokenInfo(tokenEndpoint.getAccessToken(principal, parameters).getBody()); } @PostMapping("/token") public Oauth postAccessToken(Principal principal, @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException { return tokenInfo(tokenEndpoint.postAccessToken(principal, parameters).getBody()); } private Oauth tokenInfo(OAuth2AccessToken accessToken) { DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken; token.getAdditionalInformation().remove("jti"); LinkedHashMap<String, Object> data = new LinkedHashMap<>(token.getAdditionalInformation()); data.put("accessToken", token.getValue()); return Oauth.build(data); } }
UserAuthMapper.java
public interface UserAuthMapper extends BaseMapper<UserAuth> { }
Oauth.java
@Data @AllArgsConstructor @JsonInclude(JsonInclude.Include.NON_NULL) public class Oauth { private int code; private String message; private Object data; public static Oauth build(Object data) { return new Oauth(200, "成功", data); } }
UserAuth.java
@Data @TableName("user_auths") public class UserAuth { private String userId; private String identityType; private String identifier; private String credential; }
OauthTokenEnhancer.java
public class OauthTokenEnhancer implements TokenEnhancer { @Override public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) { User user = (User) authentication.getPrincipal(); Map<String, Object> map = new LinkedHashMap<>(); map.put("client_id", authentication.getOAuth2Request().getClientId()); map.put("username", user.getUsername()); ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(map); return accessToken; } }
UserAuthService.java
@Service public class UserAuthService implements UserDetailsService { @Autowired private UserAuthMapper userAuthMapper; @Override public UserDetails loadUserByUsername(String username) { QueryWrapper<UserAuth> queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(UserAuth::getIdentifier, username); UserAuth userAuth = userAuthMapper.selectOne(queryWrapper); if (userAuth == null) { throw new UsernameNotFoundException("账号不存在"); } List<GrantedAuthority> list = AuthorityUtils.commaSeparatedStringToAuthorityList("ROOT_USER"); return new User(userAuth.getIdentifier(), userAuth.getCredential(), list); } }
OauthDemoApplication.java
@SpringBootApplication @MapperScan("com.scut.oauthdemo.mapper") public class OauthDemoApplication { public static void main(String[] args) { SpringApplication.run(OauthDemoApplication.class, args); } }
这篇关于OauthDemo的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略