Este conteúdo não está disponível no idioma selecionado.
16.16. Web Application Security
If ModeShape is being used within a web application, then it is probably desirable to reuse the security infrastructure of the application server. This can be accomplished by implementing the SecurityContext interface with an implementation that delegates to the HttpServletRequest. Then, for each request, create a SecurityContextCredentials instance around your SecurityContext, and use these credentials to obtain a JCR Session.
Here is an example of the SecurityContext implementation that uses the servlet request:
@Immutable
public class ServletSecurityContext implements SecurityContext {
private final String userName;
private final HttpServletRequest request;
/**
* Create a {@link ServletSecurityContext} with the supplied
* {@link HttpServletRequest servlet information}.
*
* @param request the servlet request; may not be null
*/
public ServletSecurityContext( HttpServletRequest request ) {
this.request = request;
this.userName = request.getUserPrincipal() != null ? request.getUserPrincipal().getName() : null;
}
/**
* Get the name of the authenticated user.
* @return the authenticated user's name
*/
public String getUserName() {
return userName;
}
/**
* Determine whether the authenticated user has the given role.
* @param roleName the name of the role to check
* @return true if the user has the role and is logged in; false otherwise
*/
boolean hasRole( String roleName ) {
request.isUserInRole(roleName);
}
/**
* Logs the user out of the authentication mechanism.
* For some authentication mechanisms, this will be implemented as a no-op.
*/
public void logout() {
}
}
Then use this to create a Session:
HttpServletRequest request = ...
Repository repository = engine.getRepository("my repository");
SecurityContext securityContext = new ServletSecurityContext(httpServletRequest);
ExecutionContext servletContext = context.with(securityContext);
We'll see later how this can be used to obtain a JCR Session for the authenticated user.