java ee 6 - ServletRequestListener - Getting the userprincipal returns null -
i'm having web-application secured http-basic auth. implemented filter using servletrequestlistener
interface. when filter calls requestinitialized
method, getuserprincipal-method of request returns null. when check request headers, authorization-header set encrypted value. here's code:
@override public void requestinitialized(servletrequestevent e) { httpservletrequest request = (httpservletrequest) e.getservletrequest(); //p null principal p = request.getuserprincipal(); enumeration<string> enh = request.getheaders("authorization"); while (enh.hasmoreelements()) { string s = enh.nextelement(); system.out.println(s); //prints. //basic c3rhy2tvdmvyzmxvdzptexbhc3n3b3jk } }
why userprincipal not initialized?
you not setting needed security layers embedded-jetty.
here's example found in jetty embedded examples source tree.
package org.eclipse.jetty.embedded; import java.util.collections; import java.util.hashset; import java.util.set; import org.eclipse.jetty.security.constraintmapping; import org.eclipse.jetty.security.constraintsecurityhandler; import org.eclipse.jetty.security.hashloginservice; import org.eclipse.jetty.security.loginservice; import org.eclipse.jetty.security.authentication.basicauthenticator; import org.eclipse.jetty.server.server; import org.eclipse.jetty.util.security.constraint; public class securedhellohandler { public static void main(string[] args) throws exception { server server = new server(8080); loginservice loginservice = new hashloginservice("myrealm","src/test/resources/realm.properties"); server.addbean(loginservice); constraintsecurityhandler security = new constraintsecurityhandler(); server.sethandler(security); constraint constraint = new constraint(); constraint.setname("auth"); constraint.setauthenticate( true ); constraint.setroles(new string[]{"user", "admin"}); constraintmapping mapping = new constraintmapping(); mapping.setpathspec( "/*" ); mapping.setconstraint( constraint ); set<string> knownroles = new hashset<string>(); knownroles.add("user"); knownroles.add("admin"); security.setconstraintmappings(collections.singletonlist(mapping), knownroles); security.setauthenticator(new basicauthenticator()); security.setloginservice(loginservice); security.setstrict(false); // handler (or servlet) should secured hellohandler hh = new hellohandler(); security.sethandler(hh); server.start(); server.join(); } }
Comments
Post a Comment