标签:java return Autowired OauthDemo private new public
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);
}
}
标签:java,return,Autowired,OauthDemo,private,new,public 来源: https://www.cnblogs.com/Junkman/p/16516060.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。