Filter X j_security_check

2 respostas
V

Olá.

Ao fazer o login na minha aplicação com o JAAS, gostaria de realizar algumas operações antes de entrar na página propriamente dita, e para isto uso um Filter.
Só que de vez em quando dá um erro 404, “the requested resource (/aplicacao/j_security_check) is not available”.

Coloquei um breakpoint nesta classe Filter e antes de terminar de executar totalmente o código dela, o browser já direciona para a página de erro.

web.xml

<filter>
		<filter-name>securityFilter</filter-name>
		<filter-class>br.org.b.security.SecurityFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>securityFilter</filter-name>
		<servlet-name>teste</servlet-name>
	</filter-mapping>
	<filter-mapping>
		<filter-name>securityFilter</filter-name>
		<servlet-name>updatePass</servlet-name>
	</filter-mapping>
	<filter-mapping>
		<filter-name>securityFilter</filter-name>
		<servlet-name>action</servlet-name>
	</filter-mapping>
	<filter-mapping>
		<filter-name>securityFilter</filter-name>
		<url-pattern>/login.jsp</url-pattern>
	</filter-mapping>

Classe Filter

public void doFilter(ServletRequest sreq, ServletResponse sres, FilterChain chain) throws IOException, ServletException {
		HttpServletResponse response = (HttpServletResponse) sres;
		HttpServletRequest request = (HttpServletRequest) sreq;
		try {
			ManagerProfile manager = new ManagerProfile();
			UserProfileVO userProfileVO = (UserProfileVO)request.getSession().getAttribute(Constantes.USER_PROFILE_VO);
			if (userProfileVO == null){
				userProfileVO = manager.getUserProfileVO("publico");
				request.getSession().setAttribute(Constantes.USER_PROFILE_VO, userProfileVO);
			}
			Principal user = request.getUserPrincipal();
			// carrega as informações do usuario
			if ((user != null && userProfileVO == null) || (user != null && userProfileVO != null && !userProfileVO.getUserName().equals(user.getName()))){
				userProfileVO = manager.getUserProfileVO(user.getName());
				request.getSession().setAttribute(Constantes.USER_PROFILE_VO,userProfileVO);
				
				userProfileVO.setIP(request.getRemoteAddr());
                userProfileVO.setHost(request.getRemoteHost());
                Sistema sistema = (Sistema)userProfileVO.getSistemas().iterator().next();
                userProfileVO.setCoSeqSistema(Integer.toString(sistema.getCoSeqSistema()));
                request.setAttribute(Constantes.PARAMETER_SYSTEM, userProfileVO.getCoSeqSistema());
                
                manager.addFuncionalidadesSistema(userProfileVO, userProfileVO.getCoSeqSistema());
			}
			
			// se houver usuario logado
			if (userProfileVO != null && !userProfileVO.getUserName().equalsIgnoreCase("publico")){
				String moduloLogin = request.getSession().getServletContext().getInitParameter("moduloLogin");
				if ("true".equals(moduloLogin)){
					// se estiver bloqueado, redireciona pra tela de erro
					if (userProfileVO.isBloqueado()){
						// desativa a sessão e redireciona
						request.getSession().invalidate();
						response.sendRedirect(request.getContextPath() + "/security/bloqueado.jsp");
						return;
					}else
					// se a alteração da senha estiver ocorrido a mais de 60 dias, redireciona para alteração
					if (userProfileVO.getUltimaAlteracaoSenha() == null || Utils.difDays(userProfileVO.getUltimaAlteracaoSenha(), new Date()) >= 60){
						// atualizando senha
						if (!request.getRequestURI().equals(request.getContextPath() + "/j_security_update")){
							// guarda pagina requisitada anteriormente
							request.getSession().setAttribute("paginaAnterior", request.getRequestURI());
							response.sendRedirect(request.getContextPath() + "/j_security_update");
							return;
						}
					}
				}else{
					if (!StringUtils.isEmpty(userProfileVO.getCodDesbloqueio())){
						// atualizando senha - primeiro acesso
						if (!request.getRequestURI().equals(request.getContextPath() + "/j_security_update")){
							// guarda pagina requisitada anteriormente
							response.sendRedirect(request.getContextPath() + "/j_security_update?op=codigoDesbloqueio");
							return;
						}
					}
				}
			}
			
			if (request.getRequestURI().equals(request.getContextPath() + "/login.jsp")){
            	response.sendRedirect(request.getContextPath() + userProfileVO.getPaginaInicial());
			}else{
				chain.doFilter(request, response);
			}
			
		} catch (SecurityException se) {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, request.getRequestURI());
		} catch (Exception e){
			LoggerGenerator.error("Erro ao autenticar o usuario",e);
			request.getSession().removeAttribute(Constantes.USER_PROFILE_VO);
			response.sendRedirect(Constantes.getInstance().getPaginaErro());
		}

	}

Login.jsp

<form onsubmit="return valida();" name="login" method="POST" action="<%=request.getContextPath()%>/j_security_check">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="485" height="322">
  <tr>
    <td width="100%" height="35">&nbsp;</td>
  </tr>
  <tr>
    <td width="485" height="253" align="center">
    <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" height="253"  align="center">
      <tr>
        <td width="42%" height="253">
        <table border="0" cellpadding="2" style="border-collapse: collapse" bordercolor="#111111" width="70%">
          <tr>
            <td width="100%" colspan="2" align="right">
            <img border="0" src="<%=request.getContextPath()%>/Imagens/logo_login.gif" width="215" height="77"></td>
          </tr>
          <tr>
				<td width="100%" align="left"><strong><font size="1"><div id="erros"></div></font></strong>&nbsp;</td>
          </tr>
          <tr>
		    <td align="right" width="100%">
			  Usuário:&nbsp;<input type="text" onKeyPress="f1(event);" name="j_username" size="15">
			  <script>document.login.j_username.focus();</script>
          	</td>
		  </tr>
          <tr>
		    <td align="right" width="100%">
			  Senha:&nbsp;<input type="password" onKeyPress="f1(event);" name="j_password" maxlength="20" size="15">
            </td>
		  </tr>
          <tr>
            <td align="right" width="100%">
			  <input type="submit" style="width:80px" value="Login" width="80px" name="btLogar"><br>
			</td>
          </tr>
        </table>
        </td>
      </tr>
    </table>
    </td>
  </tr>
  <tr>
    <td width="100%" height="32">&nbsp;</td>
  </tr>
</table>
</form>

Obrigada,
Viviane

2 Respostas

F

O erro 404 geralmente aparece quando o usuário está autenticado e realiza requisição “j_security_check” novamente.

silvars

E como resolver este problema amigo.

Acontece comigo na primeira requisição… A validação é feita e direcionada para esse erro 404 (j_security_check)

Como resolver isso!?

Criado 9 de julho de 2007
Ultima resposta 17 de mai. de 2008
Respostas 2
Participantes 3