Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. // org.springframework.cloud:spring-cloud-starter-oauth2
  2. // org.springframework.boot:spring-boot-starter-data-jpa
  3. // com.h2database:h2
  4. // redefine: spring-security.version == 4.1.0.RELEASE
  5. package com.example;
  6.  
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.CommandLineRunner;
  9. import org.springframework.boot.SpringApplication;
  10. import org.springframework.boot.autoconfigure.SpringBootApplication;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.data.jpa.repository.JpaRepository;
  13. import org.springframework.security.authentication.AuthenticationManager;
  14. import org.springframework.security.core.authority.AuthorityUtils;
  15. import org.springframework.security.core.userdetails.User;
  16. import org.springframework.security.core.userdetails.UserDetailsService;
  17. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  18. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  19. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  20. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  21. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  22. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  23. import org.springframework.web.bind.annotation.RequestMapping;
  24. import org.springframework.web.bind.annotation.RestController;
  25.  
  26. import javax.persistence.Entity;
  27. import javax.persistence.GeneratedValue;
  28. import javax.persistence.Id;
  29. import java.security.Principal;
  30. import java.util.Optional;
  31. import java.util.stream.Stream;
  32.  
  33. @EnableResourceServer
  34. @SpringBootApplication
  35. @RestController
  36. @EnableAuthorizationServer
  37. public class AuthServiceApplication extends AuthorizationServerConfigurerAdapter {
  38. @Bean
  39. CommandLineRunner commandLineRunner(AccountRepository accountRepository) {
  40. return args ->
  41. Stream.of("apoutsma,reactive", "jlong,spring", "pwebb,boot", "dsyer,cloud")
  42. .map(x -> x.split(","))
  43. .forEach(t -> accountRepository.save(new Account(t[0], t[1])));
  44. }
  45.  
  46. @Autowired
  47. private AuthenticationManager authenticationManager;
  48.  
  49. public static void main(String[] args) {
  50. SpringApplication.run(AuthServiceApplication.class, args);
  51. }
  52.  
  53. @RequestMapping("/user")
  54. Principal principal(Principal principal) {
  55. return principal;
  56. }
  57.  
  58. @Override
  59. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  60. clients
  61. .inMemory()
  62. .withClient("acme")
  63. .secret("acmesecret")
  64. .authorizedGrantTypes("password")
  65. .scopes("openid");
  66. }
  67.  
  68. @Override
  69. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  70. endpoints
  71. .authenticationManager(this.authenticationManager);
  72. }
  73.  
  74. @Bean
  75. UserDetailsService userDetailsService(AccountRepository accountRepository) {
  76. return username ->
  77. accountRepository.findByUsername(username)
  78. .map(account -> new User(
  79. account.getUsername(),
  80. account.getPassword(),
  81. true, true, true, true,
  82. AuthorityUtils.createAuthorityList("SCOPE_READ", "SCOPE_ADMIN")
  83. ))
  84. .orElseThrow(() -> new UsernameNotFoundException(
  85. String.format("couldn't find %s!", username)));
  86.  
  87. }
  88. }
  89.  
  90. interface AccountRepository extends JpaRepository<Account, Long> {
  91.  
  92. Optional<Account> findByUsername(String username);
  93. }
  94.  
  95. @Entity
  96. class Account {
  97.  
  98. public Account(String username, String password) {
  99. this.username = username;
  100. this.password = password;
  101. }
  102.  
  103. @Override
  104. public String toString() {
  105. return "Account{" +
  106. "id=" + id +
  107. ", username='" + username + '\'' +
  108. ", password='" + password + '\'' +
  109. '}';
  110. }
  111.  
  112. Account() { // why JPA why
  113. }
  114.  
  115. @Id
  116. @GeneratedValue
  117. private Long id;
  118. private String username, password;
  119.  
  120. public Long getId() {
  121. return id;
  122. }
  123.  
  124. public String getUsername() {
  125. return username;
  126. }
  127.  
  128. public String getPassword() {
  129. return password;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement