2017-02-22 9 views
1

WebSecurityConfigurerAdapterの単純な構成を既存のSpring REST APIプロジェクトに追加してテストしたいと考えています。しかし、春が始まると、設定がロードされません。たぶん私はアプリケーションのコンテキストに追加する必要がありますが、私はそれを行う方法がわかりません。Spring WebSecurityConfigを既存のプロジェクトに追加する方法

私はcurl localhost:8080/を常に不正な応答にすると、設定がロードされていないと思われます。それはなぜですか?または、どのようにロードする必要がありますか? 私がgithubで見た多様なプロジェクトでは、それをロードするために決して特別なことはしません!おそらく、そのサーブレットが最初に埋め込まれているからでしょうか?私はWebSecurityConfigを追加する必要が

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

@Autowired 
private UserDetailsService userDetailsService; 

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

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

@Override 
protected void configure(HttpSecurity httpSecurity) throws Exception { 
    httpSecurity 
      // we don't need CSRF because our token is invulnerable 
      .csrf().disable() 

      // don't create session 
      .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() 

      .authorizeRequests() 
      //.antMatchers(HttpMethod.OPTIONS, "/**").permitAll() 

      // allow anonymous resource requests 
      .antMatchers(
        HttpMethod.GET, 
        "/", 
        "/*.html", 
        "/favicon.ico", 
        "/**/*.html", 
        "/**/*.css", 
        "/**/*.js", 
        "/**" 
      ).permitAll() 
      .antMatchers("/auth/**").permitAll() 
      .anyRequest().authenticated(); 

    // disable page caching 
    httpSecurity.headers().cacheControl(); 
} 
} 

そして、これは私のアプリケーション

@Configuration 
@EnableConfigurationProperties 
@ComponentScan(basePackageClasses = SimpleCORSFilter.class) 
@EnableAutoConfiguration org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration.class}) 
@EntityScan(basePackages = "com.thing.model") 
@RestController 
public class Application { 


    @Bean 
    public FilterRegistrationBean filterRegistrationBean() { 
     FilterRegistrationBean registrationBean = new FilterRegistrationBean(); 
     CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); 
     registrationBean.setFilter(characterEncodingFilter); 
     characterEncodingFilter.setEncoding("UTF-8"); 
     characterEncodingFilter.setForceEncoding(true); 
     registrationBean.setOrder(Integer.MIN_VALUE); 
     registrationBean.addUrlPatterns("/*"); 
     return registrationBean; 
    }  




    public static void main(String[] args) { 
       SpringApplication application = new SpringApplication(Application.class); 
     SpringApplication.run(Application.class, args); 
    } 

    @RequestMapping("/") 
    public String home() { 
      return "Hello World"; 
    } 

のpom.xml依存

 <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-tomcat</artifactId> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-actuator</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-referencing</artifactId> 
     <version>8.2</version> 
     <type>jar</type> 
    </dependency> 
    <dependency> 
     <groupId>org.jsoup</groupId> 
     <artifactId>jsoup</artifactId> 
     <version>1.7.2</version> 
     <type>jar</type> 
    </dependency> 
    <dependency> 
     <groupId>org.postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <version>9.3-1102-jdbc41</version> 
    </dependency> 
    <dependency> 
     <groupId>org.yaml</groupId> 
     <artifactId>snakeyaml</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 

答えて

0

です:

これは、単純なWebセキュリティの設定ですアプリケーションコンテキストにtを追加するメインクラス宣言の彼のライン:

... 
@Import(WebSecurityConfig.class) 
public class Application { 
... 

私がやったもう一つは、1.4.3.RELEASEへのアップグレードSpringBootで、ルートフォルダにメインアプリケーションを置く:

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.3.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

木、例えば次のようになります。

└── com 
└── app 
    ├── Application.java 
    └── config 
     └── WebSecurityConfig.java 

これは自動的にすべて@Configurationをsonフォルダに読み込みます。

0

SpringプロジェクトでWebセキュリティを有効にする方法については、exampleをご覧ください。

関連する問題