Spring security认证与授权(二)

在第一个实例基础上我做了第二个实例。第一个例子里的登录页面是springsecurity的默认页面,这种页面比较死板,因此这个实例里我将自定义登录界面。

第二个实例:

增加登录页面login.jsp:

 

<%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>

<%@ page language="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPEhtml PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>用户登录</title>

</head>

<bodyonLoad="document.f.j_username.focus();">

<c:iftest="${not emptyparam.login_error}">

    <fontcolor="red">

        登录失败,请重试.<br/><br/>

        原因:<c:outvalue="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>

    </font>

</c:if>

<formname="f"action="<c:urlvalue='j_spring_security_check'/>"method="POST">

    <table>

        <tr>

            <td>用户名:</td>

            <td>

                <inputtype='text'name='j_username'value='<c:iftest="${not emptyparam.login_error}"><c:outvalue="${SPRING_SECURITY_LAST_USERNAME}"/></c:if>'/>

            </td>

        </tr>

        <tr>

            <td>密     码:</td>

            <td><inputtype='password'name='j_password'></td>

        </tr>

        <tr>

            <td>

                <inputtype="checkbox"name="_spring_security_remember_me"></td><td>两周内自动登录

            </td>

        </tr>

        <tr>

            <tdcolspan='2'align="center">

                <inputname="submit"type="submit"> 

                <inputname="reset"type="reset">

            </td>

        </tr>

    </table>

</form>

</body>

</html>

自己定义的登录页面,用户名必须为j_username,密码必须为j_password。

修改applicationContext-security.xml文件:

 

 

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

    xmlns:sec="http://www.springframework.org/schema/security"

    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-2.5.xsd

   http://www.springframework.org/schema/security

   http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">

    <!-- 自动配置模式,拦截所有请求,有ROLE_USER才可以通过 -->

    <sec:httpauto-config="true">

        <sec:intercept-urlpattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>

        <sec:intercept-urlpattern="/**"access="ROLE_USER"/>

        <sec:form-loginlogin-page="/login.jsp"authentication-failure-url="/login.jsp?login_error=1"/>

    </sec:http>

     <sec:authentication-provider>

            <sec:user-service>

                <sec:username="sharp"password="sharp"authorities="ROLE_USER"/>

            </sec:user-service>

    </sec:authentication-provider>

    <!-- 认证管理器。用户名密码都集成在配置文件中 -->

    <sec:authentication-manageralias="authenticationManager"/>

   

        <!-- 指定中文资源。默认命名空间是security,所以要加前缀beans: -->

     <beanid="messageSource"

       class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

        <propertyname="basename" value="classpath:org/springframework/security/messages_zh_CN"/> 

     </bean>

</beans>

当我们在浏览器地址栏里输入下面的url:http://localhost:8080/springsecurity02/login.jsp

我们就可以再浏览器里看到用户登录界面:



呵呵,这是我们自定义的登录页面。输入正确的用户名和密码,即可进入登录成功页面;否则,会出现登录错误提示。

源码下载



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值