Spring Security REST

0

Witam chciałem podpiąć standardową autoryzację na serwisie restowym w springu. Moja konfiguracja spring wygląda następująco:

@Configuration
@ComponentScan(basePackages = "org.schedule.service")
@EnableWebMvc
@Import({DatabaseSpringConfig.class, CoreSpringConfig.class})
@ImportResource({"classpath*:/WEB-INF/spring-security.xml"})
public class ServiceSpringConfig extends WebMvcConfigurationSupport{

	@Override
	protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
	    List<MediaType> jsonTypes = new ArrayList<>(jsonConverter.getSupportedMediaTypes());
	    jsonTypes.add(MediaType.TEXT_PLAIN);
	    jsonConverter.setSupportedMediaTypes(jsonTypes);
	    converters.add(jsonConverter);
	}

}

Nie operuje na najnowszym spring security i dlatego doładowuję tutaj resource z konfiguracją spring security znajdującą się w pliku xml. Oto on:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

	<security:http auto-config='true'>
		<security:intercept-url pattern="/workers/**"
			access="ROLE_USER, ROLE_ADMIN" />
		<security:intercept-url pattern="/shops/**"
			access="ROLE_USER, ROLE_ADMIN" />
		<security:intercept-url pattern="/**" access="ROLE_ADMIN" />
		<security:http-basic />
	</security:http>

	<security:authentication-manager>
		<security:authentication-provider>
			<security:user-service>
				<security:user name="username1" password="password1"
					authorities="ROLE_USER" />
				<security:user name="username2" password="password2"
					authorities="ROLE_USER, ROLE_ADMIN" />
			</security:user-service>
		</security:authentication-provider>
	</security:authentication-manager>
</beans>

Dodatkowo mój plik web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Schedule-service</display-name>
	<context-param>
		<param-name>contextClass</param-name>
		<param-value>
			org.springframework.web.context.support.AnnotationConfigWebApplicationContext
		</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>SpringDispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextClass</param-name>
			<param-value>
				org.springframework.web.context.support.AnnotationConfigWebApplicationContext
			</param-value>
		</init-param>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>org.schedule.service.config</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>SpringDispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<filter>
		<filter-name>cors</filter-name>
		<filter-class>org.schedule.service.filters.CorsFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>cors</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<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>

</web-app>

Mavenem zaimportowałem odpowiednie paczki. I nic. Nie wyrzuca mi 401 kiedy chcę się dostać do określonego zasobu, tylko wypluwa dalej dane jak gdyby nigdy nic. Proszę o jakąś wskazówkę czy takie rozwiązanie w ogóle ma prawo działać.
PS. Nie pytajcie dlaczego basic authentication to na projekt na uczelnie a czas nagli;)

0

Sprobuj przeniesc filtr na sama gure w web.xml

1 użytkowników online, w tym zalogowanych: 0, gości: 1