Guest User

Untitled

a guest
Aug 15th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. management.security.enabled=false
  2.  
  3. spring.boot.admin.client.metadata.user.name=${security.user.name}
  4. spring.boot.admin.client.metadata.user.password=${security.user.password}
  5.  
  6. spring.boot.admin.client.metadata.user.name=admin
  7. spring.boot.admin.client.metadata.user.password=example
  8.  
  9. 15-08-2018 22:19:29.987 [registrationTask1] INFO d.c.b.a.c.r.ApplicationRegistrator.register - Application registered itself as 6c0b3ada
  10. 15-08-2018 22:19:38.753 [http-nio-8080-exec-4] INFO o.s.b.a.e.m.MvcEndpointSecurityInterceptor.logUnauthorizedAttempt - Full authentication is required to access actuator endpoints. Consider adding Spring Security or set 'management.security.enabled' to false.
  11.  
  12. Antwortkopfzeilen (342 B)
  13. Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  14. Content-Length: 0
  15. Content-Type: application/json;charset=UTF-8
  16. Date: Wed, 15 Aug 2018 21:17:44 GMT
  17. Expires: 0
  18. Pragma: no-cache
  19. X-Application-Context: application:8081
  20. X-Content-Type-Options: nosniff
  21. X-Frame-Options: DENY
  22. X-XSS-Protection: 1; mode=block
  23.  
  24. Anfragekopfzeilen (494 B)
  25. Accept: application/json, text/plain, */*
  26. Accept-Encoding: gzip, deflate
  27. Accept-Language: de,en-US;q=0.7,en;q=0.3
  28. Connection: keep-alive
  29. Cookie: JSESSIONID=13C22C76A043263B3F1…f-2ab6-4ad9-b687-e37178d2701d
  30. Host: localhost:8081
  31. Referer: http://localhost:8081/
  32. User-Agent: Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/61.0
  33. X-XSRF-TOKEN: 5aae0cff-2ab6-4ad9-b687-e37178d2701d
  34.  
  35. SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
  36.  
  37. #SpringBootAdmin
  38. spring.boot.admin.url=http://localhost:8081
  39. spring.boot.admin.username=admin
  40. spring.boot.admin.password=adminPw
  41. spring.boot.admin.client.metadata.user.name=admin
  42. spring.boot.admin.client.metadata.user.password=example
  43.  
  44. management.security.enabled=true
  45. management.security.roles=ADMIN, USER
  46. management.info.git.mode=full
  47.  
  48. @Configuration
  49. @EnableWebSecurity
  50. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  51.  
  52. /* Time, in seconds, to have the browser cache static resources (one week). */
  53. private static final int BROWSER_CACHE_CONTROL = 604800;
  54.  
  55. @Bean
  56. public BCryptPasswordEncoder bCryptPasswordEncoder() {
  57. return new BCryptPasswordEncoder();
  58. }
  59.  
  60. @Override
  61. protected void configure(HttpSecurity http) throws Exception {
  62. http
  63. .authorizeRequests()
  64. //define URLs only a user with the role admin can access
  65. // NOTE: Spring Security automatically adds "ROLE_" while performing this check.
  66. .antMatchers("/admin").hasAnyRole("ADMIN", "USER")
  67. .antMatchers("/admin/**").hasAnyRole("ADMIN", "USER")
  68. .antMatchers("/*").permitAll()
  69. .and()
  70. .formLogin()
  71. .loginPage("/login").loginProcessingUrl("/login")
  72. .usernameParameter("username").passwordParameter("password")
  73. .defaultSuccessUrl("/admin")
  74. .and()
  75. .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login?logout")
  76. //rememberMe functionality stays for 4 weeks
  77.  
  78. .and().authorizeRequests().antMatchers("/console/**").hasRole("ADMIN")
  79. .and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
  80.  
  81. CharacterEncodingFilter filter = new CharacterEncodingFilter();
  82. filter.setEncoding("UTF-8");
  83. filter.setForceEncoding(true);
  84. http.addFilterBefore(filter,CsrfFilter.class);
  85.  
  86. //return 403 error instead of login page when trying to access an forbidden page
  87. //http.exceptionHandling().authenticationEntryPoint(new Http403ForbiddenEntryPoint());
  88. }
  89. }
  90.  
  91. @Component
  92. public class CustomAuthenticationProvider implements AuthenticationProvider {
  93.  
  94. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  95.  
  96. @Autowired
  97. private UserServiceImpl userService;
  98.  
  99. @Autowired
  100. private BCryptPasswordEncoder bCryptPasswordEncoder;
  101.  
  102. @Override
  103. public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  104.  
  105. String username = authentication.getName();
  106. String password = authentication.getCredentials().toString();
  107.  
  108. User user = userService.findByUsername(username);
  109.  
  110. if (Objects.nonNull(user)) {
  111.  
  112. //"found user, proceeding to check password
  113. Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
  114.  
  115.  
  116. if (bCryptPasswordEncoder.matches(password, user.getPassword())) {
  117. //password is a match
  118. //Setting up Authorities
  119. for (Role role : user.getRoles()) {
  120. logger.info("adding Role : " + role.getName());
  121. grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
  122. }
  123.  
  124. return new UsernamePasswordAuthenticationToken(username, user.getPassword(), grantedAuthorities);
  125.  
  126. } else {
  127. logger.info("Unsuccessful login attempt - wrong password.");
  128. return null;
  129. }
  130. } else {
  131. logger.info("Unsuccessful login attempt - user not found.");
  132. return null;
  133. }
  134. }
  135.  
  136. @Override
  137. public boolean supports(Class<?> authentication) {
  138. return authentication.equals(UsernamePasswordAuthenticationToken.class);
  139. }
  140. }
Add Comment
Please, Sign In to add comment