为什么我的Spring Security login.jsp puke CSS和我如何解决它? [英] Why does my Spring Security login.jsp puke CSS and how do I fix it?

查看:298
本文介绍了为什么我的Spring Security login.jsp puke CSS和我如何解决它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用Spring Security和JSP页面为旧版Java网络应用程序创建了自定义登录页面。传统应用程序不使用Spring MVC。我在MyEclipse中预览它,它应用我的CSS样式。但是当我在调试模式下运行它,并看看它与Firefox它有问题。 1)CSS不被渲染。 2)提交表单后,它被重定向到CSS文件,因此浏览器显示了CSS文件的文本!

I have created a custom login page for a legacy java web app using Spring Security and a JSP page. The legacy app does NOT use Spring MVC. I preview it in MyEclipse and it applies my CSS styles. But when I run it in debug mode and look at it with Firefox it has problems. 1) The CSS is not rendered. 2) After submitting the form, it gets redirected to the CSS file, so the browser shows the text of the CSS file!

我已经结束了的天,并没有找到一个解决方案在这个网站或任何其他。我从来没有使用过JSP或Spring Security,所以我甚至不知道罪魁祸首。任何人都可以指出我要调整的位置?

I've been over this for a couple of days, and haven't found a solution on this site or any other. I've never had to use JSP or Spring Security before, so I'm not even sure of the culprit. Can anyone point out where I'm screwing up?

我的 login.jsp 档案:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" session="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:url value="/user_pass_login" var="loginUrl"/>
<html>

<HEAD>
    <link href="Tracker.css" type="text/css" rel="stylesheet">
    <TITLE>ATP3 Login</TITLE>
</HEAD>

<body onload='document.loginForm.username.focus();'>
    <DIV id="login_body"
            align="center" >
        <h1>Sample Tracker Login</h1>

        <form id="loginForm" name="loginForm" action="${loginUrl}" method="post">
            <c:if test="${param.error != null}">
                <div class="alert alert-error">
                    Failed to login.
                    <c:if test="${SPRING_SECURITY_LAST_EXCEPTION != null}">
                                <BR /><BR />Reason: <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />
                    </c:if>
                </div>
            </c:if>
            <c:if test="${param.logout != null}">
                <div class="alert alert-success">You have been logged out.</div>
            </c:if>
            <BR />
            <label for="username">Username:</label> 
            <input 
                type="text" 
                id="username"
                name="username" />
            <BR />
            <BR />
            <label for="password">Password:  </label> 
            <input
                type="password"
                id="password" 
                name="password" />
            <BR />
            <BR />
            <div class="form-actions">
                <input 
                    id="submit" 
                    class="btn" 
                    name="submit" 
                    type="submit"
                    value="Login" />
            </div>
        </form>
    </DIV>
    </body>
</html>

登录后,我重定向到我的CSS文件的URL, Tracker.css

After logging in I get redirected to the url for my CSS file, Tracker.css:

/** Add css rules here for your application. */


/** Example rules used by the template application (remove for your app) */
h1 {
  font-size: 2em;
  font-weight: bold;
  color: #000555;
  margin: 40px 0px 70px;
  text-align: center;
}

#login_body, #formLogin {
    color: #000555;
    background-color: #F6FCED;  
}

.sendButton {
  display: block;
  font-size: 16pt;
}

/** Most GWT widgets already have a style name defined */
.gwt-DialogBox {
  width: 400px;
}

...

Santosh Joshi说,我回顾了我的Spring Security配置XML文件中的安全设置。这是发生了什么:

After thinking about what @Santosh Joshi said, I reviewed the security settings within my Spring Security configuration XML file. And here is what was happening:


  1. login.jsp 首次加载时,它无权访问 Tracker.css 文件,因为安全设置阻止它(请参阅下面的正确文件),因此页面未格式化。

  2. 成功登录后,找到了CSS文件,但由于JSP格式不正确,因此它只会显示CSS文件。

  1. When the login.jsp first loads it doesn't have access to the Tracker.css file, because the security settings prevent it (see below for the corrected file), so the page isn't formatted.
  2. When the user successfully logs in, the CSS file is found, but since the JSP is malformed, it just spews the CSS file.

修正


  1. 我修改了 login.jsp ,如下所示:
  1. I modified the login.jsp as follows:

  1. 使用xml定义删除第1行

  2. 将带有url变量声明的第4行移到表单标签正上方

  3. 在JSP指令下添加一个html文档类型定义


  • 我向Spring Security XML http标记添加了一个权限行

  • I added a permission line to the Spring Security XML http tag

    新的 login.jsp 文件

    <!DOCTYPE html>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" session="true"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    
        <HEAD>
            <link href="Tracker.css" type="text/css" rel="stylesheet">
            <TITLE>ATP3 Login</TITLE>
        </HEAD>
    
        <body onload='document.loginForm.username.focus();'>
            <DIV id="login_body"
                    align="center" >
                <h1>Sample Tracker Login</h1>
    
                <c:url value="/user_pass_login" var="loginUrl"/>
                <form id="loginForm" name="loginForm" action="${loginUrl}" method="post">
                    <c:if test="${param.error != null}">
                        <div class="alert alert-error">
                            Failed to login.
                            <c:if test="${SPRING_SECURITY_LAST_EXCEPTION != null}">
                                        <BR /><BR />Reason: <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />
                            </c:if>
                        </div>
                    </c:if>
                    <c:if test="${param.logout != null}">
                        <div class="alert alert-success">You have been logged out.</div>
                    </c:if>
                    <BR />
                    <label for="username">Username:</label> 
                    <input 
                        type="text" 
                        id="username"
                        name="username" />
                    <BR />
                    <BR />
                    <label for="password">Password:  </label> 
                    <input
                        type="password"
                        id="password" 
                        name="password" />
                    <BR />
                    <BR />
                    <div class="form-actions">
                        <input 
                            id="submit" 
                            class="btn" 
                            name="submit" 
                            type="submit"
                            value="Login" />
                    </div>
                </form>
            </DIV>
        </body>
    </html>
    

    这是来自安全XML文件的代码段。请注意 Tracker.css 的新拦截网址标记:

    Here is a snippet from the security XML file. Note the new intercept-url tag for Tracker.css:

    <!-- This is where we configure Spring-Security  -->
    <http auto-config="true" 
            use-expressions="true" 
            disable-url-rewriting="true">
        <intercept-url pattern="/"
            access="permitAll"/>
        <intercept-url pattern="/login*"
            access="permitAll"/>
        <intercept-url pattern="/login*/*"
            access="permitAll"/>
        <intercept-url pattern="/favicon.ico"
            access="permitAll"/>
        <intercept-url pattern="/Tracker.css"
            access="permitAll"/>
        <intercept-url pattern="/**"
            access="hasAnyRole('ROLE_ADMIN','ROLE_WRITE','ROLE_READ')"/>
        <form-login login-page="/login.jsp"
            login-processing-url="/user_pass_login"
            username-parameter="username"
            password-parameter="password" />
    </http>
    


    推荐答案

    似乎CSS的路径有问题,您放置了CSS

    Seems a problem with the path of the CSS, where have you placed the CSS

    < link href =Tracker.csstype =text / css =stylesheet/> ;

    <link href="Tracker.css" type="text/css" rel="stylesheet"/>

    您还可以使用firebug在Firefox中调试您的应用程序,如果没有应用,那么这意味着CSS没有正确下载只是检查控制台在Firefox for 404或其他错误。也吱吱在Firefox实际想要得到这里(URLFirefox指的是)CSS。

    you can also debug your application in Firefox using firebug, if it's not applying then it means that CSS has not downloaded properly just check console in Firefox for 404 or other errors. Also cheek where is Firefox actually wants to get this( the URLFirefox is referring) CSS .

    这篇关于为什么我的Spring Security login.jsp puke CSS和我如何解决它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆