J'essaie d'utiliser Spring Security 3.0.5 dans mon application Web. Fondamentalement, je veux avoir un service Web qui retourne des données au format json via HTTP GET
.Traitement du message d'erreur non autorisé pour l'authentification de base dans Spring Security
J'ai implémenté un service RESTful qui renvoie des données lorsque l'adresse http://localhost:8080/webapp/json
est demandée. Cela fonctionne très bien avec la commande boucle suivante
> curl http://localhost:8080/webapp/json
{"key":"values"}
Après avoir ajouté l'authentification de base en utilisant la sécurité de printemps, je peux utiliser les commandes suivantes pour obtenir les données
> curl http://localhost:8080/webapp/json
<html><head><title>Apache Tomcat/6.0.29 - Error report .....
> curl -u username:password http://localhost:8080/webapp/json
{"key":"values"}
La première renvoie commande page standard d'erreur tomcat depuis maintenant il nécessite un nom d'utilisateur et un mot de passe. Ma question est de savoir s'il est possible de gérer l'accès refusé de telle sorte qu'il imprime mon propre message d'erreur? dire
> curl http://localhost:8080/webapp/json
{"error":"401", "message":"Username and password required"}
Voici ma configuration de sécurité du printemps et AccessDeniedHandler
. Comme vous pouvez le voir, j'essaie d'ajouter access-denied-handler
qui imprime simplement une chaîne par l'intermédiaire de la réponse de servlet mais il n'imprime toujours pas mon propre message sur la ligne de commande.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<global-method-security secured-annotations="enabled"/>
<beans:bean name="access-denied" class="webapp.error.JSONAccessDeniedHandler"></beans:bean>
<http auto-config="true">
<access-denied-handler ref="access-denied"/>
<intercept-url pattern="/json" access="ROLE_USER,ROLE_ADMIN" />
<http-basic />
</http>
<authentication-manager>
<authentication-provider>
<password-encoder hash="md5"/>
<user-service>
...
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
AccessDeniedHandler.java
package webapp.error;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
public class JSONAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
PrintWriter writer = response.getWriter();
writer.print("{\"error\":\"401\", \"message\":\"Username and password required\"}");
}
}
Pouvez-vous vérifier la sortie de boucle S ou -v boucle? – Raghuram
J'ai modifié ma question. Ça n'imprimait rien mais ça n'imprime pas ce que je veux. La curl -S et curl -v impriment tous deux la page d'erreur standard tomcat 401. -v a plus de détails sur la demande/réponse – gigadot