Spring security, Angular1.4, POST, CORS
Bonjour à tous,
j'ai un gros problème technique, je vous explique rapidement pas à pas pour être clair:
Je n'arrivais pas à passer un bean contenant une liste depuis mon FrontEnd (Angular 1.4) vers mon BackEnd (sping 4, spring security)
J'ai d'abord désactivé spring security et j'ai trouvé la solution que voici :
Mon controller :
Code:
1 2 3 4 5
| @RequestMapping(value="/create/", method=RequestMethod.POST, consumes="application/json")
@ResponseBody
public StatusResponse create(@RequestBody EventsDTO events) throws TechnicalException {
return new StatusResponse();
} |
côté Angular :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| var _queryPost = function(url, data, defData) {
console.log('data : ', data);
$http({
headers: {'Content-Type': 'application/json',
'Accept': 'application/json'},
method: 'POST',
url: url,
//params: data,
data: data,
withCredentials: true
})
.success(function (data, status, headers, config) {
defData.$resolve(data);
$log.info(data);
})
.error(function (data, status, headers, config) {
$log.warn('*** DataProvider - query - error - status:' + status);
401 === status ? $location.path('/signIn') : $location.path('/error' + status);
defData.$reject(data);
});
return defData;
}; |
sauf que lorsque je réactive spring-security j'obtiens une erreur 401 !
Après de nombreuses recherches, j'ai cru comprendre que l'erreur se produit car j'envoie depuis angular un Content-Type avec la valeur "application/json"
Or les règles CORS indiquent que celui-ci doit contenir la valeur : application/x-www-form-urlencoded ou d'autres que j'ai oubliées....en tout cas, pas "application/json"
sauf que si je change la valeur du Content-type, ça passe mais j'obtiens une erreur 415 (logique car le controlleur attend un application/json)
J'ai essayé pas mal de solutions trouvées sur le net en ajoutant par exemple des infos sur le header côté Angular mais rien n'y fait ....
voici mes fichiers de config , web.xml:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| <!-- CORS related filter -->
<filter>
<filter-name>CORS</filter-name>
<filter-class>
org.eclipse.jetty.servlets.CrossOriginFilter
</filter-class>
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedMethods</param-name>
<param-value>GET,POST,DELETE,PUT,HEAD,OPTIONS</param-value>
</init-param>
<init-param>
<param-name>allowedHeaders</param-name>
<param-value>
origin, content-type, accept, authorization,
x-requested-with, access-token, x-xsrf-token
</param-value>
</init-param>
<init-param>
<param-name>supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring Security Filters -->
<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> |
spring security config :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<http create-session="stateless" use-expressions="true" entry-point-ref="customAuthenticationEntryPoint">
<intercept-url pattern="/api/**" access="isAuthenticated()" />
<remember-me key="remember-me-security" services-ref="rememberMeServices" />
<form-login
authentication-success-handler-ref="customAuthenticationSuccessHandler "
authentication-failure-handler-ref="customAuthenticationFailureHandler " />
<logout success-handler-ref="customLogoutSuccessHandler"/>
</http>
<beans:bean id="rememberMeServices" class="org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices">
<beans:property name="userDetailsService" ref="customUserDetailService"/>
<beans:property name="key" value="remember-me-security"/>
<beans:property name="cookieName" value="X-XSRF-TOKEN" />
<beans:property name="tokenValiditySeconds" value="3600" />
<!-- <property name="parameter" value="remember-me-parameter"/> -->
</beans:bean>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="customUserDetailService">
<password-encoder hash="sha-256">
<!-- <salt-source user-property="nom"/> -->
</password-encoder>
</authentication-provider>
</authentication-manager>
<global-method-security secured-annotations="enabled" pre-post-annotations="enabled"/>
</beans:beans> |
Comment passer cet obstacle qui bloque tout mon travail ?? Merci d'avance