Play framework 小知识点


Secure模块

提供了简单的验证功能,只要使用@With(Secure.class)标注在Controller上即可!

1.需要验证:

应用中99%的功能,都必须要登陆后才能访问,将这些功能所在的Controller都添加@With(Secure.class)。

当访问Controller中的任何方法时就会进行登录检查。【注:play在Secure类的checkAccess()上使用 @Before(unless={"login", "authenticate", "logout"}),将这3个方法排除了,不会拦截】

 

2.不需要验证:

如,登录页面,权限检查,登出动作,注册页面,这些都不需要验证。

play提供了一个登录页面,需要加入注册页面,那么就需要新建一个Controller,并且不能被Secure进行检查。该Controller不使用@With(Secure.class),注册页面就不会被拦截了。

 

login页面的路径显示问题

如果没有为secure模块指定路由,URL显示为

http://localhost:9000/secure/login

 

如果为Secure模块指定了路由(Secure模块提供了一系列默认的路由配置):

#Import Secure routes

*       /             module:secure

则URL显示为:

http://localhost:9000/login

 

 

改造play的登陆页面,加入注册按钮

play secure:ov --login

修改app\views\Secure\login.html,添加注册按钮

Html代码   收藏代码
  1. #{extends 'Secure/layout.html' /}  
  2.   
  3.   
  4. <div id="login">  
  5.       
  6.     <h1>&{'secure.title'}</h1>  
  7.           
  8.     #{form @authenticate()}  
  9.           
  10.         #{if flash.error}  
  11.             <p class="error">  
  12.                 &{flash.error}  
  13.             </p>  
  14.         #{/if}  
  15.         #{if flash.success}  
  16.             <p class="success">  
  17.                 &{flash.success}  
  18.             </p>  
  19.         #{/if}  
  20.       
  21.         <p id="username-field">  
  22.             <label for="username">&{'secure.username'}</label>  
  23.             <input type="text" name="username" id="username" value="${flash.username}" />  
  24.         </p>  
  25.         <p id="password-field">  
  26.             <label for="password">&{'secure.password'}</label>  
  27.             <input type="password" name="password" id="password" value="" />  
  28.         </p>  
  29.         <p id="remember-field">  
  30.             <input type="checkbox" name="remember" id="remember" value="true" ${flash.remember ? 'checked="true"' : ''} />  
  31.             <label for="remember">&{'secure.remember'}</label>  
  32.         </p>  
  33.         <p id="signin-field">  
  34.             <input type="submit" id="signin" value="&{'secure.signin'}" />  
  35.             <!-- 加入注册按钮 -->  
  36.             <input type="button" id="reg" value="注册"/>  
  37.         </p>  
  38.     #{/form}  
  39. </div>  

 

复制Secure模块下的secure.css,重命名为login.css,放到public\stylesheets目录下

这样做的目的是,不对play内部的样式文件进行修改,而是拷贝单独一份进行使用

修改login.css,目的在于将2个按钮放在一行显示

 

Html代码   收藏代码
  1. /*修改样式,将2个按钮放在一行*/  
  2. #signin-field input {  
  3.     display: inline;  
  4.     margin-top: 10px;  
  5.     margin-left: 90px;  
  6. }  
  7.   
  8. /*用户名框与密码框等长*/  
  9. #username-field input {  
  10.     width: 150px;  
  11.     font-size: 13px;  
  12. }  

 

 

同样,将login.html所依赖的layout.html一起拷贝到项目中

play secure:ov --layout

修改引入的样式文件,指向刚才那个新的login.css,这样就脱离了对play内部的依赖。

 

Html代码   收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
  4.     <head>  
  5.         <title>Login</title>          
  6.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  7.         <!-- 使用修改后的样式文件 -->  
  8.         <link rel="stylesheet" type="text/css" media="screen" href="@{'/public/stylesheets/login.css'}" />  
  9.     </head>  
  10.     <body>  
  11.         #{doLayout /}   
  12.     </body>  
  13. </html>  

 

效果:



 

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值