Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableResourceServer
  4. @SessionAttributes("authorizationRequest")
  5. public class AuthApplication extends WebMvcConfigurerAdapter {
  6.  
  7. public static void main(String[] args) {
  8. SpringApplication.run(AuthApplication.class, args);
  9. }
  10.  
  11. @Override
  12. public void addViewControllers(ViewControllerRegistry registry) {
  13. registry.addViewController("/login")
  14. .setViewName("forward:/index.html");
  15. registry.addViewController("/oauth/confirm_access")
  16. .setViewName("authorize");
  17.  
  18. }
  19. @Configuration
  20. @Order(-20)
  21. protected static class SecurityConfig extends WebSecurityConfigurerAdapter {
  22.  
  23. @Autowired
  24. private AuthenticationManager authenticationManager;
  25.  
  26. @Override
  27. protected void configure(HttpSecurity http) throws Exception {
  28. http
  29. .formLogin().loginPage("/login").permitAll()
  30. .and().authorizeRequests()
  31. .anyRequest().authenticated();
  32. }
  33.  
  34. @Override
  35. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  36. auth.parentAuthenticationManager(authenticationManager);
  37. }
  38.  
  39. }
  40. @Configuration
  41. @EnableAuthorizationServer
  42. protected static class OAuth2AuthorizationConfig extends
  43. AuthorizationServerConfigurerAdapter {
  44.  
  45. @Autowired
  46. private AuthenticationManager authenticationManager;
  47.  
  48. @Bean
  49. public JwtAccessTokenConverter jwtAccessTokenConverter() {
  50. JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
  51. KeyPair keyPair = new KeyStoreKeyFactory(
  52. new ClassPathResource("keystore.jks"),
  53. "foobar".toCharArray())
  54. .getKeyPair("test");
  55. converter.setKeyPair(keyPair);
  56. return converter;
  57. }
  58.  
  59. @Override
  60. public void configure(ClientDetailsServiceConfigurer clients) throws
  61. Exception {
  62. clients.inMemory()
  63. .withClient("acme")
  64. .secret("acmesecret")
  65. .authorizedGrantTypes("authorization_code", "refresh_token",
  66. "password").scopes("openid").autoApprove(true);
  67. }
  68.  
  69. @Override
  70. public void configure(AuthorizationServerEndpointsConfigurer endpoints)
  71. throws Exception {
  72. endpoints.authenticationManager(authenticationManager)
  73. .accessTokenConverter(
  74. jwtAccessTokenConverter());
  75. }
  76.  
  77. @Override
  78. public void configure(AuthorizationServerSecurityConfigurer oauthServer)
  79. throws Exception {
  80. oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess(
  81. "isAuthenticated()");
  82. }
  83. }
  84. }
  85.  
  86. <body>
  87. <div class="container">
  88. <form role="form" action="login" method="post">
  89. <div class="form-group">
  90. <label for="username">Username:</label>
  91. <input type="text" class="form-control" id="username"
  92. name="username"/>
  93. </div>
  94. <div class="form-group">
  95. <label for="password">Password:</label>
  96. <input type="password" class="form-control" id="password"
  97. name="password"/>
  98. </div>
  99. <input type="hidden" id="csrf_token" name="${_csrf.parameterName}"
  100. value="${_csrf.token}"/>
  101. <button type="submit" class="btn btn-primary">Submit</button>
  102. </form>
  103. </div>
  104. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement