0

私はこれをquestion alreadyにしました。私はスプリングブートで.css.jsのファイルをswaggerにロードできるようにするにはどうすればいいですか? (すでに/registerや他のパスでこれをやったとして)ここで特定のファイルタイプを許可するようにスプリングブートを設定する。

WebSecurityConfig.javaファイル

package com.path.to.project.config; 

import com.path.to.project.jwt.JwtAuthenticationEntryPoint; 
import com.path.to.project.jwt.JwtAuthenticationTokenFilter; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 
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.config.http.SessionCreationPolicy; 
import org.springframework.security.core.userdetails.UserDetailsService; 
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 
import org.springframework.security.crypto.password.PasswordEncoder; 
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; 

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private JwtAuthenticationEntryPoint unauthorizedHandler; 

    @Autowired 
    private UserDetailsService userDetailsService; 

    @Autowired 
    public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { 
     authenticationManagerBuilder.userDetailsService(userDetailsService) 
       .passwordEncoder(passwordEncoder()); 
    } 

    @Bean 
    public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception { 
     return new JwtAuthenticationTokenFilter(); 
    } 

    @Override 
    protected void configure(HttpSecurity httpSecurity) throws Exception { 
     httpSecurity 
       .csrf().disable() 
       .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() 
       .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() 
       .authorizeRequests() 
       .antMatchers("/auth/**").permitAll() 
       .antMatchers("/register/**").permitAll() 
       .antMatchers("/swagger-ui*").permitAll() 
       .antMatchers("/v2/**").permitAll() 
       .antMatchers("/sysadmin/**").hasRole("SYSADMIN") 
       .antMatchers("/admin/**").hasRole("ADMIN") 
       .antMatchers("/siteadmin/**").hasRole("SITEADMIN") 
       .anyRequest().authenticated(); 

     // Custom JWT based security filter 
     httpSecurity 
       .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class); 

    } 

    @Bean 
    public PasswordEncoder passwordEncoder() { 
     PasswordEncoder encoder = new BCryptPasswordEncoder(); 
     return encoder; 
    } 

} 
+0

をご.css.jsパスへのすべてのアクセスを許可されますが、JARまたはWARを使用してください。配備? JS/CSSファイルは現在どこにありますか? – luboskrnac

+0

何にもマッチしないリクエストは 'authenticated'でなければなりません。あなたが望むもの(認証されたものの上)にルールを追加するだけです。 –

答えて

0

ちょうど

@Override 
    protected void configure(HttpSecurity httpSecurity) throws Exception { 
     httpSecurity 
       .csrf().disable() 
       .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() 
       .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() 
       .authorizeRequests() 
       .antMatchers("/auth/**").permitAll() 
       .antMatchers("/register/**").permitAll() 
       .antMatchers("/swagger-ui*").permitAll() 
       .antMatchers("/v2/**").permitAll() 
       .antMatchers("/webjars/**").permitAll() //swagger ui files 
       .antMatchers("/sysadmin/**").hasRole("SYSADMIN") 
       .antMatchers("/admin/**").hasRole("ADMIN") 
       .antMatchers("/siteadmin/**").hasRole("SITEADMIN") 
       .anyRequest().authenticated(); 

     // Custom JWT based security filter 
     httpSecurity 
       .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class); 

    } 
+0

問題は私にはcss/jsディレクトリがありません。そのUIのない​​残りのAPI。 css/jsはswaggerによって提供されています(パスが何であるかはわかりません)。 –

+0

'.antMatchers("/webjars/** ")。permitAll()' –

+0

springfoxを使用している場合(チュートリアルから)、swaggerに必要なすべてのものはwebjarsマッピングを使って提供する必要がありますものはspringfoxで配信されます) –

関連する問題