Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Custom AuthenticationProvider never invoked - 404 error on http://localhost:8080/igloo/j_spring_security_check
    text
    copied!<p>I am trying to get an example of custom j_spring_security_check working on tomcat 7.0.47. Spring MVC goes to login page ok but gives error after clicking submit - what I am expecting is spring to fill in the user roles and then go to main.jsp. </p> <p>spring main config:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"&gt; &lt;!-- ############################################# --&gt; &lt;context:component-scan base-package="frostbyte.igloo" /&gt; &lt;!-- ############################################# --&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/"/&gt; &lt;!-- ############################################# --&gt; &lt;mvc:annotation-driven/&gt; &lt;!-- ############################################# --&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"&gt;&lt;/property&gt; &lt;property name="prefix" value="WEB-INF/jsp/"&gt;&lt;/property&gt; &lt;property name="suffix" value=".jsp"&gt;&lt;/property&gt; &lt;/bean&gt; &lt;!-- ############################################# --&gt; &lt;bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt; &lt;property name="basename" value="classpath:messages"&gt;&lt;/property&gt; &lt;property name="defaultEncoding" value="UTF-8"&gt;&lt;/property&gt; &lt;/bean&gt; &lt;!-- ############################################# --&gt; &lt;/beans&gt; </code></pre> <p>spring security config:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd"&gt; &lt;!-- ############################################# --&gt; &lt;beans:bean id="FrostByteAuthenticationProvider" class="frostbyte.igloo.jsp.custom.FrostByteAuthenticationProvider"&gt;&lt;/beans:bean&gt; &lt;authentication-manager alias="authenticationManager"&gt; &lt;authentication-provider ref="FrostByteAuthenticationProvider"&gt;&lt;/authentication-provider&gt; &lt;/authentication-manager&gt; &lt;!-- ############################################# --&gt; &lt;http auto-config="true" use-expressions="true"&gt; &lt;form-login login-processing-url="/login" login-page="/login" default-target-url="/main" username-parameter="j_username" password-parameter="j_password" authentication-failure-url="/login?auth=fail"/&gt; &lt;intercept-url pattern="/login" access="permitAll"&gt;&lt;/intercept-url&gt; &lt;intercept-url pattern="/logout" access="permitAll"&gt;&lt;/intercept-url&gt; &lt;intercept-url pattern="/**" access="hasRole('ADMIN')"/&gt; &lt;logout logout-url="/logout" logout-success-url="/logout_success"&gt;&lt;/logout&gt; &lt;/http&gt; &lt;!-- ############################################# --&gt; &lt;/beans:beans&gt; </code></pre> <p>the AuthenticationProvider (non of these log messages ever get printed) :</p> <pre><code>package frostbyte.igloo.jsp.custom; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import frostbyte.common.FrostbyteRole; import frostbyte.common.FrostbyteUser; public class FrostByteAuthenticationProvider implements AuthenticationProvider { private static final Logger LOG = Logger.getLogger(FrostByteAuthenticationProvider.class); @Override public boolean supports(Class&lt;?&gt; authentication) { LOG.error("FrostByteAuthenticationProvider : supports : Marker 1"); System.out.println("FrostByteAuthenticationProvider : supports : Marker 1"); return true; } @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { LOG.error("FrostByteAuthenticationProvider : authenticate : Marker 1"); System.out.println("FrostByteAuthenticationProvider : authenticate : Marker 1"); Authentication rtn = null; String username = authentication.getName(); String password = authentication.getCredentials().toString(); LOG.error("FrostByteAuthenticationProvider : authenticate : Marker 10 : username = "+username); LOG.error("FrostByteAuthenticationProvider : authenticate : Marker 20 : password = "+password); FrostbyteUser user = new FrostbyteUser(); //for test everything validates user.setUsername(username); user.getRoles().add(new FrostbyteRole("ADMIN")); LOG.debug("Authenticate : Marker 100"); //if (user.getUsername().equalsIgnoreCase("username")) if (true) { LOG.debug("Authenticate : Marker 200"); if (true) //if (password.equalsIgnoreCase(user.getPassword())) { LOG.debug("Authenticate : Marker 300"); List&lt;GrantedAuthority&gt; grants = new ArrayList&lt;GrantedAuthority&gt;(); for (FrostbyteRole _role:user.getRoles()) { if (_role.equals("ADMIN")) { for ( String __role : _role.getRoles() ) { grants.add(new SimpleGrantedAuthority(__role.toUpperCase())); } } } rtn = new UsernamePasswordAuthenticationToken(username, password, grants); LOG.debug("Authenticate : Marker 898,000 : rtn = "+ rtn); } LOG.debug("Authenticate : Marker 899,000 : rtn = "+ rtn); } LOG.debug("Authenticate : Marker 900,000 : rtn = "+ rtn); return rtn; } } </code></pre> <p>the login jsp:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;%@ include file="/WEB-INF/jsp/include.jsp" %&gt; &lt;meta http-equiv="pragma" content="no-cache"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;link rel="stylesheet" href="resources/styles.css" type="text/css" media="screen" /&gt; &lt;style type="text/css"&gt;&lt;/style&gt; &lt;/head&gt; &lt;body&gt; request.getAttribute("message") = &lt;%= request.getAttribute("message") %&gt; &lt;br /&gt; request.getParameter("message") = &lt;%= request.getParameter("message") %&gt; &lt;form action="&lt;c:url value = "/j_spring_security_check" /&gt;" method="post"&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt; &lt;div class="containerCenterAlign"&gt; &lt;div class="container"&gt; &lt;div class="titleClass"&gt;Igloo&lt;br /&gt;&lt;div class="errorMessage"&gt;&lt;%= message %&gt;&lt;/div&gt;&lt;/div&gt; &lt;ul class="loginUL"&gt; &lt;li class="loginLeft"&gt; &lt;label class="loginLabel" for="j_username"&gt;Username&lt;/label&gt; &lt;input id="username" name="j_username" class="loginText" type="text" value="" maxlength="150" /&gt; &lt;label class="loginLabel" for="j_password"&gt;Password&lt;/label&gt; &lt;input id="password" name="j_password" class="loginText" type="password" value="" maxlength="150" /&gt; &lt;/li&gt; &lt;li class="loginRight"&gt; &lt;input type="submit" name="submit" id="submit" value="Login" class="loginSubmit" /&gt; &lt;/li&gt; &lt;/ul&gt; &lt;div style="clear: both; height: 2px;"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>include.jsp:</p> <pre><code>&lt;%@ page language="java" contentType="text/html;charset=UTF-8"%&gt; &lt;%@ page session="false"%&gt; &lt;%@ page import="java.io.*" %&gt; &lt;%@ page import="java.util.*" %&gt; &lt;%@ page import="frostbyte.*" %&gt; &lt;%@ page import="org.apache.log4j.*" %&gt; &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; &lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %&gt; &lt;%@ taglib prefix="s" uri="http://www.springframework.org/tags"%&gt; &lt;%@ taglib prefix="sform" uri="http://www.springframework.org/tags/form"%&gt; &lt;%@ taglib prefix="frostbyte" uri="http://frostbyte/tags" %&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload