Spring Security无法在RestServices

时间:2016-11-17 15:54:51

标签: spring-security basic-authentication spring-restcontroller

我有一个RestController工作,我想学习如何使用Spring Security启用Basic Auth。

我已经创建了一个扩展WebSecurityConfigurerAdapter的类,根据我的理解,它应该足够了。可悲的是,我可以在不提供凭据的情况下调用资源。

这是我的代码:

配置程序适配器

package es.ieci.test.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    public static final String REALM = "TEST_REALM";

    @Override
    protected void configure(HttpSecurity http) throws Exception{

        http
            .csrf().disable()
            .authorizeRequests().antMatchers("/services/**").hasRole("ADMIN").and()
            .httpBasic().realmName(REALM).authenticationEntryPoint(getBasicAuthEntryPoint()).and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

    }

    @Bean
    public CustomBasicAuthenticationEntryPoint getBasicAuthEntryPoint(){
        return new CustomBasicAuthenticationEntryPoint();
    }   
}

Spring配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="es.ieci.test.security, es.ieci.test.controller, es.ieci.test.services, es.ieci.test.utils" />            

</beans>  

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>TestRestSpring</display-name> 

    <servlet>
        <servlet-name>springrest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springrest</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>      

</web-app>

我的pom文件

<dependencies>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>   
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.7</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
  </dependencies>

我正在使用Tomcat v6.0m,服务器日志看起来不错:

  2016年11月17日下午2:00:32   org.springframework.security.web.DefaultSecurityFilterChain   INFORMACIÓN:创建过滤器链:   org.springframework.security.web.util.matcher.AnyRequestMatcher@1,   [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@f671723,   org.springframework.security.web.context.SecurityContextPersistenceFilter@2b8af779,   org.springframework.security.web.header.HeaderWriterFilter@49d0b86,   org.springframework.security.web.authentication.logout.LogoutFilter@6f7b847c,   org.springframework.security.web.authentication.www.BasicAuthenticationFilter@3b022cae,   org.springframework.security.web.savedrequest.RequestCacheAwareFilter@1eebd4a2,   org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4cb106be,   org.springframework.security.web.authentication.AnonymousAuthenticationFilter@392002bb,   org.springframework.security.web.session.SessionManagementFilter@e13b2fb,   org.springframework.security.web.access.ExceptionTranslationFilter@6b4187ba,   org.springframework.security.web.access.intercept.FilterSecurityInterceptor@241377b6]

但如果我打电话给

http://localhost:8383/TestRestSpring/services/echo

如果不提供用户凭据,则服务器响应为200而不是预期的Auth错误

2 个答案:

答案 0 :(得分:0)

我已经能够获得401代码,为web.xml添加过滤器

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

但我不确定这是否正确。

答案 1 :(得分:-1)

您似乎错过了在web.xml中添加上下文配置文件,因此未完成安全配置的组件扫描。

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value>
 </context-param> 
 <listener>
        <listener-class>
                 org.springframework.web.context.ContextLoaderListener
         </listener-class>
</listener>