Ich habe Probleme mit der Einrichtung meines sicheren REST-Service. Ich wollte einen einfachen Login/Logout-Dienst erstellen und damit herumspielen.@RolesAllowed hat keine Auswirkungen auf meinen RESTful-Service
Ich folgte diesem Tutorial. Ich übersprang den Teil mit dem Login-Formular und schrieb den Benutzernamen und das Passwort fest in den Dienst. (login()) http://www.blog.btbw.pl/java/wildfly-9-login-form-simple-example/
Alles lief gut, bis ich die Annotation @RolesAllowed verwenden wollte. Also habe ich eine neue Methode (adminInfo()) mit dieser Annotation erstellt. Aber ich kam zu dem Schluss, dass die Anmerkung keinen Unterschied machte. Ich konnte es erfolgreich aufrufen, ohne mit der Rolle "ADMIN" angemeldet zu sein.
Vielleicht weiß einer von euch intelligenten Leuten draußen, was ich falsch gemacht habe und kann mir helfen. Entschuldigung für meine schlechte Grammatik, ich bin es nicht gewohnt, so viel auf Englisch zu schreiben.
Vielen Dank.
Das sind meine Dateien:
Ich bin ein einfacher Dienst verwendet, die wie folgt aussieht:
@Context
private HttpServletRequest request;
@GET
@Path("/hello")
public Response hello() {
return Response.ok().entity("Hello, World!").build();
}
@GET
@Path("/logout")
@RolesAllowed("ADMIN")
public Response adminInfo() {
return Response.ok().entity("hello " + request.getUserPrincipal().getName()).build();
}
@POST
@Path("/login")
public Response login() {
try {
request.login("admin", "admin");
return Response.ok().entity("login successful").build();
} catch (Exception e) {
return Response.status(Status.BAD_REQUEST).entity("login failed").build();
}
}
@GET
@Path("/logout")
@RolesAllowed("ADMIN")
public Response logout() {
try {
request.logout();
return Response.ok().entity("logout successful").build();
} catch (Exception e) {
return Response.status(Status.BAD_REQUEST).entity("logout failed").build();
}
}
Mein Jboss-web.xml wie folgt aussieht: Und
<jboss-web>
<context-root>/</context-root>
<security-domain>jaas-realm</security-domain>
</jboss-web>
Meine web.xml sieht wie folgt aus:
<web-app>
<security-constraint>
<web-resource-collection>
<web-resource-name>Authentication</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>ADMIN</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>jaas-realm</realm-name>
</login-config>
<security-role>
<role-name>ADMIN</role-name>
</security-role>
</web-app>
Mein Wildfly standalone.xml wird wie folgt konfiguriert:
<security-domain name="jaas-realm" cache-type="default">
<authentication>
<login-module name="login-module" code="Database" flag="required">
<module-option name="dsJndiName" value="java:/datasource"/>
<module-option name="principalsQuery" value="select password from users where username=?"/>
<module-option name="rolesQuery" value="select rolename, 'Roles' from roles where username=?"/>
<module-option name="hashAlgorithm" value="SHA-256"/>
<module-option name="hashEncoding" value="base64"/>
<module-option name="unauthenticatedIdentity" value="guest"/>
</login-module>
</authentication>
</security-domain>
Danke, ich habe eine dummes Copypasta. Ich habe es geändert, aber es funktioniert immer noch nicht. –