Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daichan4649/7f2fa938eeae46716f1d to your computer and use it in GitHub Desktop.
Save daichan4649/7f2fa938eeae46716f1d to your computer and use it in GitHub Desktop.
tomcat7 basic authentication
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// logon user name
final String userName = request.getUserPrincipal().getName();
PrintWriter out = response.getWriter();
out.println(String.format("doGet(%s)", userName));
out.flush();
out.close();
}
<!-- ADD role, user-->
<role rolename="test-role" />
<user username="user01" password="user01" roles="test-role" />
<user username="user02" password="user02" roles="test-role" />
<security-constraint>
<web-resource-collection>
<web-resource-name>User Basic Authentication</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>test-role</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>User Basic Authentication</realm-name>
</login-config>
<security-role>
<role-name>test-role</role-name>
</security-role>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment