WebSecurityConfigurer.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. *
  3. * Copyright (c) 2018-2025, lengleng All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * Neither the name of the pig4cloud.com developer nor the names of its
  14. * contributors may be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. * Author: lengleng (wangiegie@gmail.com)
  17. *
  18. */
  19. package com.pig4cloud.pigx.auth.config;
  20. import com.fasterxml.jackson.databind.ObjectMapper;
  21. import com.pig4cloud.pigx.common.security.handler.MobileLoginSuccessHandler;
  22. import com.pig4cloud.pigx.common.security.mobile.MobileSecurityConfigurer;
  23. import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService;
  24. import lombok.SneakyThrows;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.context.annotation.Bean;
  27. import org.springframework.context.annotation.Configuration;
  28. import org.springframework.context.annotation.Lazy;
  29. import org.springframework.context.annotation.Primary;
  30. import org.springframework.core.annotation.Order;
  31. import org.springframework.security.authentication.AuthenticationManager;
  32. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  33. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  34. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  35. import org.springframework.security.crypto.factory.PasswordEncoderFactories;
  36. import org.springframework.security.crypto.password.PasswordEncoder;
  37. import org.springframework.security.oauth2.provider.ClientDetailsService;
  38. import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
  39. import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  40. /**
  41. * @author lengleng
  42. * @date 2018/6/22
  43. * 认证相关配置
  44. */
  45. @Primary
  46. @Order(90)
  47. @Configuration
  48. public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
  49. @Autowired
  50. private ObjectMapper objectMapper;
  51. @Autowired
  52. private ClientDetailsService clientDetailsService;
  53. @Autowired
  54. private PigxUserDetailsService userDetailsService;
  55. @Lazy
  56. @Autowired
  57. private AuthorizationServerTokenServices defaultAuthorizationServerTokenServices;
  58. @Override
  59. @SneakyThrows
  60. protected void configure(HttpSecurity http) {
  61. http
  62. .formLogin()
  63. .loginPage("/token/login")
  64. .loginProcessingUrl("/token/form")
  65. .and()
  66. .authorizeRequests()
  67. .antMatchers(
  68. "/token/**",
  69. "/actuator/**",
  70. "/mobile/**").permitAll()
  71. .anyRequest().authenticated()
  72. .and().csrf().disable()
  73. .apply(mobileSecurityConfigurer());
  74. }
  75. /**
  76. * 不拦截静态资源
  77. *
  78. * @param web
  79. */
  80. @Override
  81. public void configure(WebSecurity web) {
  82. web.ignoring().antMatchers("/css/**");
  83. }
  84. @Bean
  85. @Override
  86. @SneakyThrows
  87. public AuthenticationManager authenticationManagerBean() {
  88. return super.authenticationManagerBean();
  89. }
  90. @Bean
  91. public AuthenticationSuccessHandler mobileLoginSuccessHandler() {
  92. return MobileLoginSuccessHandler.builder()
  93. .objectMapper(objectMapper)
  94. .clientDetailsService(clientDetailsService)
  95. .passwordEncoder(passwordEncoder())
  96. .defaultAuthorizationServerTokenServices(defaultAuthorizationServerTokenServices).build();
  97. }
  98. @Bean
  99. public MobileSecurityConfigurer mobileSecurityConfigurer() {
  100. MobileSecurityConfigurer mobileSecurityConfigurer = new MobileSecurityConfigurer();
  101. mobileSecurityConfigurer.setMobileLoginSuccessHandler(mobileLoginSuccessHandler());
  102. mobileSecurityConfigurer.setUserDetailsService(userDetailsService);
  103. return mobileSecurityConfigurer;
  104. }
  105. /**
  106. * https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-updated
  107. * Encoded password does not look like BCrypt
  108. *
  109. * @return PasswordEncoder
  110. */
  111. @Bean
  112. public PasswordEncoder passwordEncoder() {
  113. return PasswordEncoderFactories.createDelegatingPasswordEncoder();
  114. }
  115. }