Java Code Examples for javax.servlet.http.HttpServletRequest#authenticate()

The following examples show how to use javax.servlet.http.HttpServletRequest#authenticate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CallAuthenticatedServlet.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (!req.authenticate(resp)) {
        return;
    }

    KeycloakSecurityContext sc = (KeycloakSecurityContext) req.getAttribute(KeycloakSecurityContext.class.getName());
    if (sc == null) { // assert sc not null
        throw new AssertionError("Keycloak security context is null.");
    }
    resp.setContentType("text/html");
    PrintWriter pw = resp.getWriter();
    pw.printf("<html><head><title>%s</title></head><body>", "Customer Portal");
    pw.println("Stian Thorgersen");
    pw.println("Bill Burke");
    pw.print("</body></html>");
    pw.flush();

}
 
Example 2
Source File: MCRRequestAuthenticationFilter.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    Optional<HttpSession> httpSession = Optional.ofNullable(req.getSession(false));
    if (httpSession.map(s -> s.getAttribute(SESSION_KEY)).isPresent() && req.getUserPrincipal() == null) {
        LogManager.getLogger().info("request authentication required for: {}", req.getRemoteUser());
        req.authenticate((HttpServletResponse) response);
    }
    chain.doFilter(request, response);
}