Advertisement
Guest User

Untitled

a guest
Sep 6th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. @SpringBootApplication
  2. @EnableResourceServer
  3. @RestController
  4. public class Application {
  5.  
  6. public static void main(String[] args) {
  7. SpringApplication.run(Application.class, args);
  8. }
  9.  
  10. @RequestMapping("/home")
  11. public String home() {
  12. return "Hello World";
  13. }
  14.  
  15. @RequestMapping("/reg/a")
  16. public String reg() {
  17. return "REGISTERED";
  18. }
  19.  
  20. @RequestMapping(value = "/", method = RequestMethod.POST)
  21. @ResponseStatus(HttpStatus.CREATED)
  22. public String create(@RequestBody MultiValueMap<String, String> map) {
  23. return "OK";
  24. }
  25.  
  26. @Configuration
  27. @EnableAuthorizationServer
  28. protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
  29.  
  30. @Autowired
  31. private AuthenticationManager authenticationManager;
  32.  
  33. @Override
  34. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  35. endpoints.authenticationManager(authenticationManager);
  36. }
  37.  
  38. @Override
  39. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
  40. security.checkTokenAccess("isAuthenticated()");
  41. }
  42.  
  43. @Override
  44. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  45. clients.inMemory()
  46. .withClient("my-client-with-secret")
  47. .authorizedGrantTypes("client_credentials", "password")
  48. .authorities("ROLE_CLIENT")
  49. .scopes("read")
  50. .resourceIds("oauth2-resource")
  51. .secret("secret");
  52. }
  53.  
  54. }}
  55.  
  56. @Configuration
  57. @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
  58. public class OAuth2WebSecurityConfig extends WebSecurityConfigurerAdapter {
  59.  
  60. @Bean(name="authenticationManager")
  61. @Override
  62. public AuthenticationManager authenticationManagerBean() throws Exception {
  63. return super.authenticationManager();
  64. }
  65.  
  66. @Override
  67. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  68. String password = "pass";
  69. String user = "user";
  70. auth.inMemoryAuthentication()
  71. .withUser(user).password(password).roles("USER")
  72. .and().withUser("admin").password("admin").roles("ADMIN");
  73.  
  74. }
  75.  
  76. @Override
  77. protected void configure(HttpSecurity http) throws Exception {
  78. http
  79. .requestMatchers().antMatchers("/reg/**")
  80. .and()
  81. .authorizeRequests()
  82. .antMatchers("/reg/**").access("hasRole('ADMIN')");
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement