WebSecurityConfiguration.java 6.38 KB
Newer Older
fangxinjiang's avatar
fangxinjiang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2020 yifu4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yifu.cloud.plus.v1.yifu.auth.config;

fangxinjiang's avatar
fangxinjiang committed
19
import com.fasterxml.jackson.databind.ObjectMapper;
fangxinjiang's avatar
fangxinjiang committed
20
import com.yifu.cloud.plus.v1.yifu.auth.filter.PasswordDecoderFilter;
fangxinjiang's avatar
fangxinjiang committed
21 22
import com.yifu.cloud.plus.v1.yifu.auth.handler.YifuAuthenticationFailureHandlerImpl;
import com.yifu.cloud.plus.v1.yifu.auth.handler.YifuClientLoginSuccessHandler;
fangxinjiang's avatar
fangxinjiang committed
23 24 25 26 27 28
import com.yifu.cloud.plus.v1.yifu.common.security.component.YifuDaoAuthenticationProvider;
import com.yifu.cloud.plus.v1.yifu.common.security.grant.CustomAppAuthenticationProvider;
import com.yifu.cloud.plus.v1.yifu.common.security.handler.FormAuthenticationFailureHandler;
import com.yifu.cloud.plus.v1.yifu.common.security.handler.SsoLogoutSuccessHandler;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
fangxinjiang's avatar
fangxinjiang committed
29
import lombok.extern.log4j.Log4j2;
fangxinjiang's avatar
fangxinjiang committed
30 31
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
fangxinjiang's avatar
fangxinjiang committed
32 33
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
fangxinjiang's avatar
fangxinjiang committed
34
import org.springframework.context.annotation.Lazy;
fangxinjiang's avatar
fangxinjiang committed
35 36 37 38 39 40 41 42 43
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
fangxinjiang's avatar
fangxinjiang committed
44 45 46
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
fangxinjiang's avatar
fangxinjiang committed
47
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
fangxinjiang's avatar
fangxinjiang committed
48
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
fangxinjiang's avatar
fangxinjiang committed
49 50 51 52 53 54
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

/**
 * @author lengleng
 * @date 2022/1/12 认证相关配置
 */
fangxinjiang's avatar
fangxinjiang committed
55
@Log4j2
fangxinjiang's avatar
fangxinjiang committed
56 57 58 59 60
@Primary
@Order(90)
@Configuration
@RequiredArgsConstructor
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
fangxinjiang's avatar
fangxinjiang committed
61 62 63 64 65 66 67 68 69 70 71 72 73
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private ClientDetailsService clientDetailsService;
	@Lazy
	@Autowired
	private AuthorizationServerTokenServices defaultAuthorizationServerTokenServices;

	@Autowired
	private CacheManager cacheManager;

	@Autowired
	private TokenStore tokenStore;
fangxinjiang's avatar
fangxinjiang committed
74 75 76 77

	@Override
	@SneakyThrows
	protected void configure(HttpSecurity http) {
fangxinjiang's avatar
fangxinjiang committed
78 79
		http.addFilterBefore(getPasswordDecoderFilter(),PasswordDecoderFilter.class)
				.formLogin().loginPage("/token/login").loginProcessingUrl("/token/form")
fangxinjiang's avatar
fangxinjiang committed
80 81
				.failureHandler(authenticationFailureHandler()).and().logout()
				.logoutSuccessHandler(logoutSuccessHandler()).deleteCookies("JSESSIONID").invalidateHttpSession(true)
fangxinjiang's avatar
fangxinjiang committed
82
				.and()
83
				.authorizeRequests().antMatchers("/**/login","/token/**", "/actuator/**", "/mobile/**", "/oauth/token","/weixin/callback").permitAll()
fangxinjiang's avatar
fangxinjiang committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
				.anyRequest().authenticated().and().csrf().disable();
	}

	/**
	 * 自定义 provider 列表注入
	 * @param auth AuthenticationManagerBuilder
	 */
	@Override
	protected void configure(AuthenticationManagerBuilder auth) {
		YifuDaoAuthenticationProvider daoAuthenticationProvider = new YifuDaoAuthenticationProvider();
		daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());

		// 处理默认的密码模式认证
		auth.authenticationProvider(daoAuthenticationProvider);
		// 自定义的认证模式
		auth.authenticationProvider(new CustomAppAuthenticationProvider());
	}

	@Bean
	@Override
	@SneakyThrows
	public AuthenticationManager authenticationManagerBean() {
		return super.authenticationManagerBean();
	}

	/**
	 * 认证中心静态资源处理
	 * @param web WebSecurity
	 */
	@Override
	public void configure(WebSecurity web) {
		web.ignoring().antMatchers("/css/**", "/error");
	}

	/**
	 * sso 表单登录失败处理
	 * @return FormAuthenticationFailureHandler
	 */
	@Bean
	public AuthenticationFailureHandler authenticationFailureHandler() {
		return new FormAuthenticationFailureHandler();
	}

	/**
	 * SSO 退出逻辑处理
	 * @return LogoutSuccessHandler
	 */
	@Bean
	public LogoutSuccessHandler logoutSuccessHandler() {
		return new SsoLogoutSuccessHandler();
	}

	/**
	 * 密码处理器
	 * @return 动态密码处理器 {类型}密文
	 */
	@Bean
	public PasswordEncoder passwordEncoder() {
		return PasswordEncoderFactories.createDelegatingPasswordEncoder();
	}

fangxinjiang's avatar
fangxinjiang committed
145 146 147 148 149 150 151 152 153 154 155
	/**
	* @author fxj
	* @date 2022/5/27 17:22
	*/
	public PasswordDecoderFilter getPasswordDecoderFilter(){
		PasswordDecoderFilter filter = new PasswordDecoderFilter();
		try {
			filter.setAuthenticationManager(this.authenticationManager());
		}catch (Exception e){
			log.error("WebSecurityConfigure>>>>>",e);
		}
fangxinjiang's avatar
fangxinjiang committed
156 157
		filter.setAuthenticationSuccessHandler(yifuClientLoginSuccessHandler());
		filter.setAuthenticationFailureHandler(yifuAuthenticationFailureHandler());
fangxinjiang's avatar
fangxinjiang committed
158 159
		return filter;
	}
fangxinjiang's avatar
fangxinjiang committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

	private AuthenticationFailureHandler yifuAuthenticationFailureHandler() {
		return YifuAuthenticationFailureHandlerImpl.builder()
				.objectMapper(objectMapper).build();
	}

	private AuthenticationSuccessHandler yifuClientLoginSuccessHandler() {
		return YifuClientLoginSuccessHandler.builder()
				.objectMapper(objectMapper)
				.clientDetailsService(clientDetailsService)
				.passwordEncoder(passwordEncoder())
				.cacheManager(cacheManager)
				.tokenStore(tokenStore)
				.defaultAuthorizationServerTokenServices(defaultAuthorizationServerTokenServices).build();
	}
fangxinjiang's avatar
fangxinjiang committed
175
}