SecurityConfig.java 19.3 KB
Newer Older
PWF-WK01\pengwufeng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*
 * Copyright 2002-2016 the original author or authors.
 *
 * 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.beecode.inz.war;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy;
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
import org.springframework.security.web.context.SecurityContextPersistenceFilter;
import org.springframework.security.web.session.ConcurrentSessionFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

import com.beecode.amino.metadata.runtime.MetadataRuntime;
import com.beecode.bap.user.service.BapUserService;
import com.beecode.bcp.authc.handler.RestLogoutHandler;
import com.beecode.bcp.core.context.AminoContextConstants;
import com.beecode.bcp.type.KClass;
import com.beecode.bcp.web.context.AminoContextPersistenceFilter;
import com.beecode.bcp.web.context.AminoContextRepository;
import com.beecode.bcp.web.context.AminoContextService;
import com.beecode.inz.authentication.filter.INZTenantAuthenticationFilter;
import com.beecode.inz.authentication.filter.InzConcurrentSessionFilter;
import com.beecode.inz.authentication.filter.InzWebAuthenticationFilter;
import com.beecode.inz.authentication.handler.RESTAppAuthenticationSuccessHandler;
import com.beecode.inz.authentication.handler.RESTAuthenticationEntryPoint;
import com.beecode.inz.authentication.handler.RESTAuthenticationFailureHandler;
import com.beecode.inz.authentication.handler.RESTAuthenticationSuccessHandler;
55
import com.beecode.inz.authentication.handler.RESTWarehouseUserAuthenticationSuccessHandler;
王衍超 committed
56
import com.beecode.inz.authentication.handler.SandUserAuthenticationSuccessHandler;
PWF-WK01\pengwufeng committed
57
import com.beecode.inz.authentication.provider.AppUserAuthenticationProvider;
王衍超 committed
58
import com.beecode.inz.authentication.provider.SandUserAuthProvider;
PWF-WK01\pengwufeng committed
59
import com.beecode.inz.authentication.provider.UserAuthenticationProvider;
60
import com.beecode.inz.authentication.provider.WarehouseUserAuthenticationProvider;
王衍超 committed
61 62
import com.beecode.inz.basis.context.sand.SandUserContextPersistenceFilter;
import com.beecode.inz.basis.context.sand.SandUserContextRepository;
63 64
import com.beecode.inz.basis.context.warehouse.WarehouseUserContextPersistenceFilter;
import com.beecode.inz.basis.context.warehouse.WarehouseUserContextRepository;
PWF-WK01\pengwufeng committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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

/**
 * @author Joe Grandja
 */
@EnableWebSecurity
public class SecurityConfig  {
	
	@Autowired
	private MetadataRuntime metadataRuntime;
	
	@Autowired
	@Qualifier("com.beecode.bap.user.internal.service.BapUserServiceImpl")
	private BapUserService bapUserService;
	
	@Autowired
	private AminoContextService aminoContextService;
	
	@Autowired
	@Qualifier(AminoContextConstants.ORGENITYNAME)
	private KClass orgClass ;
	
	@Autowired
	@Qualifier(AminoContextConstants.STAFFENTITYNAME)
	private KClass staffClass;
	
	@Autowired
	private RESTAuthenticationEntryPoint authenticationEntryPoint;
	
	@Autowired
	private RESTAuthenticationFailureHandler authenticationFailureHandler;
	
	@Autowired
	private RESTAuthenticationSuccessHandler authenticationSuccessHandler;
	
//	@Autowired
//	private CompanyInfoAuthenticationFailureHandler companyInfoAuthenticationFailureHandler;
//	
//	@Autowired
//	private CompanyInfoAuthenticationSuccessHandler companyInfoauthenticationSuccessHandler;
	
	@Autowired
	private RESTAppAuthenticationSuccessHandler appAuthenticationSuccessHandler;
	
	@Autowired
	private AppUserAuthenticationProvider appUserAuthenticationProvider;
	
	@Autowired
	private UserAuthenticationProvider userAuthenticationProvider;
	
114 115 116 117 118 119 120 121
	@Autowired
	private RESTWarehouseUserAuthenticationSuccessHandler warehouseUserAuthenticationSuccessHandler;
	
	@Autowired
	private WarehouseUserAuthenticationProvider warehouseUserAuthenticationProvider;
	
	@Autowired
	private WarehouseUserContextRepository warehouseUserContextRepository;
王衍超 committed
122 123 124 125 126 127 128 129 130 131
	
	//采砂用户
	@Autowired
	private SandUserAuthenticationSuccessHandler sandUserAuthenticationSuccessHandler;
	
	@Autowired
	private SandUserAuthProvider sandUserAuthProvider;
	
	@Autowired
	private SandUserContextRepository sandUserContextRepository;
PWF-WK01\pengwufeng committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

	@Autowired
	private RestLogoutHandler CLogoutHandler;
	
//	@Autowired
//	SecretAuthenticationProvider secretAuthenticationProvider;
	
//	@Autowired
//	private RegisterSessionAuthenticationStrategy registerSessionAuthenticationStrategy;
	
	@Autowired
	private SessionAuthenticationStrategy concurrentSessionControlAuthenticationStrategy;
	
	@Autowired
	private SessionRegistry sessionRegistry;
	
	@Autowired
	private InzLogoutHandler logoutHandler;
	
	@Autowired
	private AminoContextRepository repository;
	
154

PWF-WK01\pengwufeng committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	@Configuration
	@Order(1)
	public class InzAppSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

		@Override
		protected void configure(HttpSecurity http) throws Exception {
			
			List<SessionAuthenticationStrategy> delegateStrategies = new ArrayList<SessionAuthenticationStrategy>();
			//delegateStrategies.add(registerSessionAuthenticationStrategy);
			delegateStrategies.add(concurrentSessionControlAuthenticationStrategy);
			http.sessionManagement().maximumSessions(1);
			http.csrf().disable();
			http.cors().disable();
			http.antMatcher("/app/login").authorizeRequests().anyRequest().authenticated();
			http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
			InzWebAuthenticationFilter filter = new InzWebAuthenticationFilter();
			filter.setAuthenticationSuccessHandler(appAuthenticationSuccessHandler);
			filter.setAuthenticationFailureHandler(authenticationFailureHandler);
			filter.setAuthenticationManager(authenticationManager());
			filter.setSessionAuthenticationStrategy(new CompositeSessionAuthenticationStrategy(delegateStrategies));
			RequestMatcher requestMatcher = new AntPathRequestMatcher("/app/login", "POST");
			filter.setRequiresAuthenticationRequestMatcher(requestMatcher);
		
			
			InzConcurrentSessionFilter inzConcurrentSessionFilter = new InzConcurrentSessionFilter(sessionRegistry) ;
			http.addFilterAt(inzConcurrentSessionFilter, ConcurrentSessionFilter.class);
			
			AminoContextPersistenceFilter aminoContextPersistenceFilter = new AminoContextPersistenceFilter(metadataRuntime,repository);
			aminoContextPersistenceFilter.setAminoContextService(aminoContextService);
			http.addFilterAt(filter,UsernamePasswordAuthenticationFilter.class);
			http.addFilterAfter(aminoContextPersistenceFilter, SecurityContextPersistenceFilter.class);
			http.addFilterAfter(new INZTenantAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
		}
		
		@Override
		protected void configure(AuthenticationManagerBuilder auth) throws Exception {
			auth.authenticationProvider(appUserAuthenticationProvider);
		}

		@Override
		protected AuthenticationManager authenticationManager() throws Exception {
			return super.authenticationManager();
		}
	}
	
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	@Configuration
	@Order(2)
	public class WarehouseUserSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

		@Override
		protected void configure(HttpSecurity http) throws Exception {
			
			List<SessionAuthenticationStrategy> delegateStrategies = new ArrayList<SessionAuthenticationStrategy>();
			delegateStrategies.add(concurrentSessionControlAuthenticationStrategy);
			http.sessionManagement().maximumSessions(1);
			http.csrf().disable();
			http.cors().disable();
			http.antMatcher("/warehouse/user/login").authorizeRequests().anyRequest().authenticated();
			http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
			
			InzWebAuthenticationFilter filter = new InzWebAuthenticationFilter();
			filter.setAuthenticationSuccessHandler(warehouseUserAuthenticationSuccessHandler);
			filter.setAuthenticationFailureHandler(authenticationFailureHandler);
			filter.setAuthenticationManager(authenticationManager());
			filter.setSessionAuthenticationStrategy(new CompositeSessionAuthenticationStrategy(delegateStrategies));
			RequestMatcher requestMatcher = new AntPathRequestMatcher("/warehouse/user/login", "POST");
			filter.setRequiresAuthenticationRequestMatcher(requestMatcher);
			http.addFilterAt(filter, UsernamePasswordAuthenticationFilter.class);
			
			InzConcurrentSessionFilter inzConcurrentSessionFilter = new InzConcurrentSessionFilter(sessionRegistry) ;
			http.addFilterAt(inzConcurrentSessionFilter, ConcurrentSessionFilter.class);
			
			WarehouseUserContextPersistenceFilter contextPersistenceFilter = new WarehouseUserContextPersistenceFilter(metadataRuntime, warehouseUserContextRepository);
			http.addFilterAfter(contextPersistenceFilter, SecurityContextPersistenceFilter.class);
			
			http.addFilterAfter(new INZTenantAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
		}
		
		@Override
		protected void configure(AuthenticationManagerBuilder auth) throws Exception {
			auth.authenticationProvider(warehouseUserAuthenticationProvider);
		}

		@Override
		protected AuthenticationManager authenticationManager() throws Exception {
			return super.authenticationManager();
		}
	}
	
PWF-WK01\pengwufeng committed
244 245
	@Configuration
	@Order(3)
246 247 248 249 250 251 252 253 254 255
	public class WarehouseApiConfigurerAdapter extends WebSecurityConfigurerAdapter {

		@Override
		protected void configure(HttpSecurity http) throws Exception {
			
			List<SessionAuthenticationStrategy> delegateStrategies = new ArrayList<SessionAuthenticationStrategy>();
			delegateStrategies.add(concurrentSessionControlAuthenticationStrategy);
			http.sessionManagement().maximumSessions(1);
			http.csrf().disable();
			http.cors().disable();
256
			http.antMatcher("/warehouse/api/**").authorizeRequests()
257
//			.antMatchers("/warehouse/user").permitAll()
PWF-WK01\pengwufeng committed
258
			.anyRequest().authenticated();
259
			
PWF-WK01\pengwufeng committed
260
			http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
261 262 263 264 265 266 267 268
			
			InzConcurrentSessionFilter inzConcurrentSessionFilter = new InzConcurrentSessionFilter(sessionRegistry) ;
			http.addFilterAt(inzConcurrentSessionFilter, ConcurrentSessionFilter.class);
			
			WarehouseUserContextPersistenceFilter contextPersistenceFilter = new WarehouseUserContextPersistenceFilter(metadataRuntime, warehouseUserContextRepository);
			http.addFilterAfter(contextPersistenceFilter, SecurityContextPersistenceFilter.class);
			
			http.addFilterAfter(new INZTenantAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
269

270
			http.logout().logoutUrl("/warehouse/api/logout")
271 272 273 274 275 276 277 278 279 280 281 282
			.addLogoutHandler(logoutHandler).invalidateHttpSession(true)
			.permitAll();
		}

		@Override
		protected AuthenticationManager authenticationManager() throws Exception {
			return super.authenticationManager();
		}
	}
	
	@Configuration
	@Order(4)
王衍超 committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	public class SandUserSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
		
		@Override
		protected void configure(HttpSecurity http) throws Exception {
			
			List<SessionAuthenticationStrategy> delegateStrategies = new ArrayList<SessionAuthenticationStrategy>();
			delegateStrategies.add(concurrentSessionControlAuthenticationStrategy);
			http.sessionManagement().maximumSessions(1);
			http.csrf().disable();
			http.cors().disable();
			http.antMatcher("/sand/user/login").authorizeRequests().anyRequest().authenticated();
			http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
			
			InzWebAuthenticationFilter filter = new InzWebAuthenticationFilter();
			filter.setAuthenticationSuccessHandler(sandUserAuthenticationSuccessHandler);
			filter.setAuthenticationFailureHandler(authenticationFailureHandler);
			filter.setAuthenticationManager(authenticationManager());
			filter.setSessionAuthenticationStrategy(new CompositeSessionAuthenticationStrategy(delegateStrategies));
			RequestMatcher requestMatcher = new AntPathRequestMatcher("/sand/user/login", "POST");
			filter.setRequiresAuthenticationRequestMatcher(requestMatcher);
			http.addFilterAt(filter, UsernamePasswordAuthenticationFilter.class);
			
			InzConcurrentSessionFilter inzConcurrentSessionFilter = new InzConcurrentSessionFilter(sessionRegistry) ;
			http.addFilterAt(inzConcurrentSessionFilter, ConcurrentSessionFilter.class);
			
			SandUserContextPersistenceFilter contextPersistenceFilter = new SandUserContextPersistenceFilter(metadataRuntime, sandUserContextRepository);
			http.addFilterAfter(contextPersistenceFilter, SecurityContextPersistenceFilter.class);
			
			http.addFilterAfter(new INZTenantAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
		}
		
		@Override
		protected void configure(AuthenticationManagerBuilder auth) throws Exception {
			auth.authenticationProvider(sandUserAuthProvider);
		}
		
		@Override
		protected AuthenticationManager authenticationManager() throws Exception {
			return super.authenticationManager();
		}
	}
	
	@Configuration
	@Order(5)
	public class SandApiConfigurerAdapter extends WebSecurityConfigurerAdapter {
		
		@Override
		protected void configure(HttpSecurity http) throws Exception {
			
			List<SessionAuthenticationStrategy> delegateStrategies = new ArrayList<SessionAuthenticationStrategy>();
			delegateStrategies.add(concurrentSessionControlAuthenticationStrategy);
			http.sessionManagement().maximumSessions(1);
			http.csrf().disable();
			http.cors().disable();
杨清松 committed
337 338
			http.antMatcher("/sand/user/**").authorizeRequests()
//			.antMatchers("/sand/user").permitAll()
王衍超 committed
339 340 341 342 343 344 345 346 347 348 349 350
			.anyRequest().authenticated();
			
			http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
			
			InzConcurrentSessionFilter inzConcurrentSessionFilter = new InzConcurrentSessionFilter(sessionRegistry) ;
			http.addFilterAt(inzConcurrentSessionFilter, ConcurrentSessionFilter.class);
			
			SandUserContextPersistenceFilter contextPersistenceFilter = new SandUserContextPersistenceFilter(metadataRuntime, sandUserContextRepository);
			http.addFilterAfter(contextPersistenceFilter, SecurityContextPersistenceFilter.class);
			
			http.addFilterAfter(new INZTenantAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
			
杨清松 committed
351
			http.logout().logoutUrl("/sand/user/logout")
王衍超 committed
352 353 354 355 356 357 358 359 360 361 362 363
			.addLogoutHandler(logoutHandler).invalidateHttpSession(true)
			.permitAll();
		}
		
		@Override
		protected AuthenticationManager authenticationManager() throws Exception {
			return super.authenticationManager();
		}
	}
	
	@Configuration
	@Order(6)
PWF-WK01\pengwufeng committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	public  class WebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
		protected void configure(HttpSecurity http) throws Exception {
			
			http.csrf().disable();
			http.cors().disable();
			http.sessionManagement().maximumSessions(1);
			http.authorizeRequests()
			.antMatchers("/api/login").permitAll()
			.antMatchers("/actuator/info").permitAll()
			.antMatchers("/import/download/**").permitAll()
			.antMatchers("/basis/enterprise/**").permitAll()
			.antMatchers("/biz/download/**").permitAll()
			.antMatchers("/basis/attachments/**").permitAll()
			.antMatchers("/verification/sendCode/**").permitAll()
			.antMatchers("/verification/verifyCode/**").permitAll()
			.antMatchers(HttpMethod.POST, "/qrcode").permitAll()
			.antMatchers(HttpMethod.GET, "/qrcode/authced/**").permitAll()
			.antMatchers("/bcp/workflow/**").permitAll()
			.antMatchers("/authc/user/modifyPassword").permitAll()
			.antMatchers("/authc/user/getTelephone").permitAll()
			.antMatchers("/code/send/**").permitAll()
			.antMatchers("/code/verify/**").permitAll()
			.antMatchers("/user/regist/**").permitAll()
			.antMatchers("/basis/viewConfig/com.beecode.inz.i18n/content").permitAll()
			.antMatchers("/ops/**").permitAll()
			.antMatchers("/query/functionQuery").permitAll()
			.antMatchers("/load/**").permitAll()
			.antMatchers("/user/regist").permitAll()
			.antMatchers("/enroll/**").permitAll()
			.antMatchers("/deposit/**").permitAll()
			.antMatchers("/offerRecord/**").permitAll()
			.antMatchers("/complaint/**").permitAll()
//			.antMatchers("/consult/**").permitAll()
			.antMatchers("/code/verify").permitAll()
			.antMatchers("/code/send/**").permitAll()
			.antMatchers("/api/ssoauth/connect").permitAll()
			.antMatchers("/api/rtdcccm/**").permitAll()
			.antMatchers("/dict/listAll").permitAll()
			.antMatchers("/dict/criteria").permitAll()
			.antMatchers("/map/**").permitAll()
			.antMatchers("/loadAuctionByAsset/**").permitAll()
			.antMatchers("/crm/load/announcement/byAssetPackage/**").permitAll()
			.antMatchers("/crm/assetInfo/byAssetPackage/**").permitAll()
			.antMatchers("/authc/user/modifySelfPassword").permitAll()
			.antMatchers("/crm/load/publicity/byAssetPackage/**").permitAll()
			.antMatchers("/workflow/api/**").permitAll()
410
			.antMatchers("/dnaserver/**").permitAll()
411
			.antMatchers("/dinasBiz/organization/queryOrganizationByUserName").permitAll()
PWF-WK01\pengwufeng committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
			.anyRequest().authenticated();//listAll,modifySelfPassword,loadAuctionByAsset临时开放
			http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
			InzWebAuthenticationFilter filter = new InzWebAuthenticationFilter();
			filter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
			filter.setAuthenticationFailureHandler(authenticationFailureHandler);
			filter.setAuthenticationManager(authenticationManager());
			
			InzConcurrentSessionFilter inzConcurrentSessionFilter = new InzConcurrentSessionFilter(sessionRegistry) ;
			http.addFilterAt(inzConcurrentSessionFilter, ConcurrentSessionFilter.class);
			
			AminoContextPersistenceFilter aminoContextPersistenceFilter = new AminoContextPersistenceFilter(metadataRuntime);
			aminoContextPersistenceFilter.setAminoContextService(aminoContextService);
			http.addFilterAt(filter,UsernamePasswordAuthenticationFilter.class);
			http.addFilterAfter(aminoContextPersistenceFilter, SecurityContextPersistenceFilter.class);
			
			http.addFilterAfter(new INZTenantAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
			http.logout().logoutUrl("/logout")
			.addLogoutHandler(logoutHandler).invalidateHttpSession(true)
			.permitAll();
		}

		@Override
		protected void configure(AuthenticationManagerBuilder auth) throws Exception {
			//TODO 先替换成手机号也能登录的版本
			auth.authenticationProvider(userAuthenticationProvider);
		}

		@Override
		protected AuthenticationManager authenticationManager() throws Exception {
			return super.authenticationManager();
		}
		
	}
}