Я использую Spring Security в своем веб-приложении, аутентификация работает нормально. После входа в систему я перенаправлен на домашнюю страницу, имя пользователя, вошедшего в систему, отображается в моем приложении, и все хорошо, кроме одной вещи. У меня есть метод загрузки в моем приложении, где пользователь может загрузить видео в azure хранилище, а затем сохранить URL в базе данных. Это метод загрузки
public String fileUpload(File fileUp, String fileN) {
try {
fileN = fileN.replace(" ", "_");
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionStringU);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Get a reference to a container.
// The container name must be lower case
CloudBlobContainer container = blobClient.getContainerReference("filescontainer");
System.out.println("exist " + container.exists());
// Create the container if it does not exist.
container.createIfNotExists();
// Allow Public Access
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
// Include public access in the permissions object.
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
// Set the permissions on the container.
container.uploadPermissions(containerPermissions);
// Create or overwrite the blob with contents from a local file.
CloudBlockBlob blob = container.getBlockBlobReference(fileN);
ServiceProperties serviceProperties = blob.getServiceClient().downloadServiceProperties();
serviceProperties.setDefaultServiceVersion("2019-07-07");
blob.getServiceClient().uploadServiceProperties(serviceProperties);
/* // Plan B
* RequestOptions RequestOptions =
* blob.getServiceClient().getDefaultRequestOptions();
* // <Can Set Timeout Here>
* RequestOptions.setTimeoutIntervalInMs(?);
*/
// Used StreamWriteSize to break the file into blocks to avoid timeout
blob.setStreamWriteSizeInBytes(1024*1024);
FileInputStream in = new FileInputStream(fileUp);
blob.upload(in, fileUp.length());
return containerUrl+fileN;
} catch (Exception e) {
// Output the stack trace.
e.printStackTrace();
}
return "";
}
И это мой контроллер загрузки * 1004. *
@PostMapping("/addVideo")
public String uploadMultipleFiles(@RequestParam("vdLength") String vdLength,
@RequestParam("files1") MultipartFile files1){
byte[] buffer = new byte[4096];
int readByteCount = 0;
VideoFile vd = new VideoFile();
File target = new File(ownerID + fileNameVd + "_" + date.getTime() + ".mp4");
try(BufferedInputStream in= new BufferedInputStream(files1.getInputStream());
FileOutputStream out = new FileOutputStream(target)) {
while((readByteCount = in.read(buffer)) != -1) {
out.write(buffer, 0, readByteCount);
}
out.close();
}
vd.setVideoURL(new UploadAzurController().fileUpload(target,ownerID + fileNameVd + "_" + date.getTime() + ".mp4"));
target.delete();
videoService.addVideo(vd);}
Загрузка иногда работает, а иногда и показывает 502 - веб-сервер получил неверный ответ, выступая в качестве шлюза или прокси-сервера. И я нахожу в трассировке стека это:
2020-04-23T11: 01: 46.390408136Z 11: 01: 46.382 [http-nio-80-exe c -3] DEBUG org.springframework .security.web.access.intercept.FilterSecurityInterceptor - Ранее аутентифицировано: org.springframework.security.authentication. AnonymousAuthenticationToken@dab9512f: Принципал: anonymousUser; Полномочия: [ЗАЩИЩЕНО]; Аутентифицировано: правда; Подробности: org.springframework.security.web.authentication. WebAuthenticationDetails@ffff4c9c: RemoteIpAddress: 172.16.1.1; SessionId: null; Предоставленные полномочия: ROLE_ANONYMOUS 2020-04-23T11: 01: 46.391507942Z 11: 01: 46.391 [http-nio-80-exe c -3] DEBUG org.springframework.security.access.vote.AffirtivationBased - Voter: org. springframework.security.web.access.expression. WebExpressionVoter@1928c84c, возвращено: -1 2020-04-23T11: 01: 46.393975556Z 11: 01: 46.393 [http-nio-80-exe c -3] DEBUG org. springframework.security.web.access.ExceptionTranslationFilter - доступ запрещен (пользователь анонимный); перенаправление на точку входа аутентификации 2020-04-23T11: 01: 46.393991456Z org.springframework.security.access.AccessDeniedException: доступ запрещен 2020-04-23T11: 01: 46.393996856Z по адресу org.springframework.security.access.vote.AffirtivationBased .decide (AffirrativeBased. java: 84) 2020-04-23T11: 01: 46.394001156Z в org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation (AbstractSecurityInterceptor. java: 233) 2020-04-23T11: 01: 46.394005056Z в org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke (FilterSecurityInterceptor. java: 124) 2020-04-23T11: 01: 46.394008856Z в org.springframework.security.wecess. intercept.FilterSecurityInterceptor.doFilter (FilterSecurityInterceptor. java: 91) 2020-04-23T11: 01: 46.394012656Z по адресу org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (Filter 10-04in * 334 * 260 *: 334) прокси. -23T11: 01: 46.394016356Z в org.springframework.security.web.access.ExceptionTranslationFilter.doFilt er (ExceptionTranslationFilter. java: 119) 2020-04-23T11: 01: 46.394020156Z в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy. java: 334) 2020-04-23T11: 01 : 46.394023856Z в org.springframework.security.web.session.SessionManagementFilter.doFilter (SessionManagementFilter. java: 137) 2020-04-23T11: 01: 46.394027656Z в org.springframework.security.web.FilterinFilterChater (FilterChainProxy. java: 334) 2020-04-23T11: 01: 46.394031356Z по адресу org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter (AnonymousAuthenticationFilter. java: 111) 2020-04-23T11: 01: 46.394035156Z по адресу org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy. java: 334) 2020-04-23T11: 01: 46.394038956Z по адресу org.springframework.security.web.olil.ReterServer.Reter SecurityContextHolderAwareRequestFilter. java: 170) 2020-04-23T11: 01: 46.394042856Z в org.springframe work.security.web. : 63) 2020-04-23T11: 01: 46.394050356Z в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy. java: 334) 2020-04-23T11: 01: 46.394054756Z в орг. springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter (AbstractAuthenticationProcessingFilter. java: 200) 2020-04-23T11: 01: 46.394058656Z в org.springframework.security.web.FilterChainProxyFirFinFoxFirFirFinFoxFirFinFoxFirFinFoxFirFinFoxChinProxyFirFinFoxFormFoxFormFoxChin ProCheinFoxFirFinFoxChin ProChein Proxy. : 334) 2020-04-23T11: 01: 46.394062356Z в org.springframework.security.web.authentication.logout.LogoutFilter.doFilter (LogoutFilter. java: 116) 2020-04-23T11: 01: 46.394066156Z в организации .springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy. java: 334) 2020-04-23T11: 01: 46.394076756Z at org.springframework.security.web.header.HeaderWriterFilterInter *: 74) 2020-04-23T11: 01: 46.394080756Z по адресу org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter. java: 107) 2020-04-23T11: 01: 46.394084556Z по адресу org.springframe. security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy. java: 334) 2020-04-23T11: 01: 46.394088256Z at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter (SecurityContextPersistenceFilter. java: 105) 2020-04-23T11: 01: 46.394092156Z в org.springframework.security.web.FilterChainChainFinFirFinFinChFF java: 334) 2020-04-23T11: 01: 46.394095856Z по адресу org.springframework.security.web.context.request.asyn c .WebAsyncManagerIntegrationFilter.doFilterInternal (WebAsyncManagerIntegrationFilter. * 10-04-56): 23T11: 01: 46.394099656Z по адресу org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter. java: 107) 2020-04-23T11: 01: 46.394103456Z по адресу org.springframework.secil.Filter. doFilter (FilterChainProxy. java: 334) 2020-04-23T11: 01: 46.394107256Z в org.springframework.security.web.FilterChainProxy.doFilterInternal (FilterChainProxy. java: 215) 2020-04-23T11: 01: 46.39419 Z at org.springframework.security.web.FilterChainProxy.doFilter (FilterChainProxy. java: 178) 2020-04-23T11: 01: 4 6.394114656Z at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate (DelegatingFilterProxy. java: 357) 2020-04-23T11: 01: 46.394118456Z в org.springframework.web.filter.DelegatingFilterFilterFilter *: 270) 2020-04-23T11: 01: 46.394122156Z в орг. apache .catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain. java: 193) 2020-04-23T11: 01: 46.394125856Z в орг. apache .catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain. java: 166) 2020-04-23T11: 01: 46.394129556Z в org.springframework.web.filter.RequestContextFilter.doFilterInternal: 99. ) 2020-04-23T11: 01: 46.394133657Z в org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter. java: 107) 2020-04-23T11: 01: 46.394137457Z в орг. apache .catina .core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain. java: 193) 2020-04-23T11: 01: 46.394141157Z в организации. apache .catalina.core.ApplicationFilterChain.doFilter (Appl icationFilterChain. java: 166) 2020-04-23T11: 01: 46.394144957Z at org.springframework.web.filter.FormContentFilter.doFilterInternal (FormContentFilter. java: 92) 2020-04-23T11: 01: 46.394148657Z в org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter. java: 107) 2020-04-23T11: 01: 46.394152357Z в орг. apache .catalina.core.ApplicationFilterChain.internalFilter : 193) 2020-04-23T11: 01: 46.394156057Z в орг. apache .catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain. java: 166) 2020-04-23T11: 01: 46.394159857Z в орг.спринговой структуре .web.filter.HiddenHttpMethodFilter.doFilterInternal (HiddenHttpMethodFilter. java: 93) 2020-04-23T11: 01: 46.394166457Z в org.springframework.web.filter.OncePerRequestFilter.doFilter 04-23T11: 01: 46.394170157Z в орг. apache .catalina.core.ApplicationFilterChain. internalDoFilter (ApplicationFilterChain. java: 193) 2020-04-23T11: 01: 46.394173957Z в орг. apache .catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain. java: 166) 2020-04-23T11: 01 : 46.394177757Z в орг. apache .catalina.core.StandardWrapperValve.invoke (StandardWrapperValve. java: 200) 2020-04-23T11: 01: 46.394181757Z в орг. apache .catalina.core.StandardContextValve.alve. (StandardContextValve. java: 96) 2020-04-23T11: 01: 46.394185657Z в орг. apache .catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase. java: 490) 2020-04-23T11: 01: 46.394189357Z в орг. apache .catalina.core.StandardHostValve.invoke (StandardHostValve. java: 139) 2020-04-23T11: 01: 46.394193057Z в орг. apache .catalina.valves.ErrorReportValve.invoke ( ErrorReportValve. java: 92) 2020-04-23T11: 01: 46.394196757Z в орг. apache .catalina.core.StandardEngineValve.invoke (StandardEngineValve. java: 74) 2020-04-23T11: 01: 46.394200457 Z at org. apache .catalina.connector.CoyoteAdapter.service (CoyoteAdapter. java: 343) 2020 -04-23T11: 01: 46.394204057Z в орг. apache .coyote.http11.Http11Processor.service (Http11Processor. java: 408) 2020-04-23T11: 01: 46.394207857Z в орг. apache .coyote .AbstractProcessorLight.process (AbstractProcessorLight. java: 66) 2020-04-23T11: 01: 46.394211557Z в орг. apache .coyote.AbstractProtocol $ ConnectionHandler.process (AbstractProtocol. java: 834) 2020-04- 23T11: 01: 46.394215157Z в орг. apache .tomcat.util. net .NioEndpoint $ SocketProcessor.doRun (NioEndpoint. java: 1415) 2020-04-23T11: 01: 46.394219357Z в орг. apache .tomcat.util. net .SocketProcessorBase.run (SocketProcessorBase. java: 49) 2020-04-23T11: 01: 46.394223057Z в java .util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor. * 1105 : 1149) 2020-04-23T11: 01: 46.394226757Z в java .util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor. java: 624) 2020-04-23T11: 01: 46.394230457Z в орг. apache .tomcat.util.threads.TaskThread $ WrappingRunnable.run (TaskThread. java: 61) 2020-04-23T11: 01: 46.394234257Z в java .lang.Thread.ru n (Тема. java: 748)
Это мой класс конфигурации безопасности sping
@EnableWebSecurity
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
@Autowired
@Qualifier("datasource")
private DataSource dataSource;
public static Boolean anon;
@Value("${role.anonymous}")
public void setAnon(Boolean anon) {
this.anon = anon;
}
// Secure the endpoins with HTTP Basic authentication
@Override
protected void configure(HttpSecurity http) throws Exception {
if (anon) {
http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/Search/**").permitAll();
}
http.authorizeRequests()
.antMatchers("/manager*").hasAnyAuthority("ADMIN", "MANAGER")
.antMatchers("/uploadFile").hasAnyAuthority("ADMIN", "MANAGER")
.antMatchers("/resources/**").permitAll()
.antMatchers("/api/**").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/footer**").permitAll()
.antMatchers("/header**").permitAll()
.antMatchers("/login*").permitAll()
.anyRequest().authenticated()
.and().csrf().disable().formLogin()
.loginPage("/login").defaultSuccessUrl("/");
}
@Bean
@Override
public UserDetailsService userDetailsServiceBean() {
try {
return super.userDetailsServiceBean();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Bean
public SwitchUserFilter switchUserFilter() {
SwitchUserFilter filter = new SwitchUserFilter();
filter.setUserDetailsService(userDetailsServiceBean());
filter.setUsernameParameter("username");
filter.setSwitchUserUrl("/switch_user");
filter.setExitUserUrl("/switch_user_exit");
filter.setTargetUrl("/");
return filter;
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).authoritiesByUsernameQuery(
"Select auth.username, auth.authority , 1 as enabled from (select A.username, A.authority from admin as A UNION select M.username, M.authority from manager as M UNION select U.username, U.authority from user as U UNION select R.username, R.authority from readeruser as R) auth WHERE auth.username = ? ")
.usersByUsernameQuery(
"Select auth.username, auth.password , 1 as enabled from (select A.username, A.password ,1 as enabled from admin as A UNION select M.username, M.password ,1 as enabled from manager as M UNION select U.username, U.password ,1 as enabled from user as U UNION select R.username, R.password ,1 as enabled from readeruser as R) auth WHERE auth.username = ? ");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
StrictHttpFirewall firewall = new StrictHttpFirewall();
firewall.setAllowUrlEncodedSlash(true);
return firewall;
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/*.css");
web.ignoring().antMatchers("/*.js");
web.ignoring().antMatchers("/*.png");
web.ignoring().antMatchers("/videos/*.mp4");
web.ignoring().antMatchers("/videos/*.png");
web.ignoring().antMatchers("/videos/*.vtt");
web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
}
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*"); // this allows all origin
config.addAllowedHeader("*"); // this allows all headers
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}
После сбоя загрузки, и я go в приложении обнаружил, что пользователь все еще вошел в систему. Я надеюсь, что предоставил всю необходимую информацию, чтобы вы могли мне помочь.