Facebook
From Mammoth Agouti, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 221
  1. @SpringBootApplication
  2. @EnableAuthorizationServer
  3. @EnableResourceServer
  4. public class Application extends SpringBootServletInitializer {
  5.     public static void main(String[] args) {
  6.         SpringApplication.run(Application.class, args);
  7.     }
  8. }
  9.  
  10. /////////////////////////////////
  11.  
  12. @RestController
  13. public class HelloWorldRestController {
  14.  
  15.     @RequestMapping(value = "/", method = RequestMethod.GET)
  16.     public String lol1() {
  17.         return "lol1";
  18.     }
  19.  
  20.     @RequestMapping(value = "/user/me", method = RequestMethod.GET)
  21.     public List<User> listAllUsers() {
  22.         List<User> users = new ArrayList<>();
  23.  
  24.         User user = new User();
  25.         user.setId(1);
  26.         user.setLol("Lol");
  27.  
  28.         users.add(user);
  29.  
  30.         return users;
  31.     }
  32. }
  33.  
  34. //////////////////////////////////
  35.  
  36. @Configuration
  37. public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
  38.    
  39.     @Autowired
  40.     private AuthenticationManager authenticationManager;
  41.  
  42.     @Override
  43.     public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
  44.         oauthServer.tokenKeyAccess("permitAll()")
  45.                 .checkTokenAccess("isAuthenticated()");
  46.     }
  47.  
  48.     @Override
  49.     public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  50.         clients.inMemory()
  51.                 .withClient("SampleClientId")
  52.                 .secret("secret")
  53.                 .authorizedGrantTypes("password", "refresh_token")
  54.                 .scopes("user_info")
  55.                 .autoApprove(true) ;
  56.     }
  57.  
  58.     @Override
  59.     public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  60.         endpoints.authenticationManager(authenticationManager);
  61.     }
  62. }
  63.  
  64. ///////////////////////////////
  65.  
  66. @Configuration
  67. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  68.  
  69.     @Autowired
  70.     public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
  71.         auth.inMemoryAuthentication()
  72.                 .withUser("bill").password("abc123").roles("ADMIN").and()
  73.                 .withUser("bob").password("abc123").roles("USER");
  74.     }
  75.  
  76.     @Override
  77.     protected void configure(HttpSecurity http) throws Exception {
  78.         http
  79.                 .requestMatchers()
  80.                 .antMatchers("/login")
  81.                 .and()
  82.                 .authorizeRequests()
  83.                 .anyRequest().authenticated()
  84.                 .and()
  85.                 .formLogin().permitAll();
  86.     }
  87.  
  88.     @Override
  89.     @Bean
  90.     public AuthenticationManager authenticationManagerBean() throws Exception {
  91.         return super.authenticationManagerBean();
  92.     }
  93. }
  94.