如何测试oauth2的oauth2 passwordd验证方式

关注51Testing
Web自动化测试中绕开验证码登陆的方式
发表于: 11:26 &作者:钉子尹 & 来源:51Testing软件测试网采编
推荐标签:
  Web自动化测试中登陆需验证码是很大的一个困扰。现推荐一种简单的避开验证码登陆的方式,先代码进入登录页,人工输入验证码登录后自动保存cookie,再在新的标签中登录。  具体代码如下:1 public void yiCheCarSourceManageData(HashMap&String, String& data) throws Exception{2   //打开第一个页面,请手动输入验证码3   driver.get(CommonBean.YICHEURL);4   //输入用户名密码(需自己定位输入,也可去掉该行代码手动输入5 & &YiCheLogin.yiCheLoginFun(username, password);6 & &//使用快捷键Ctrl+t,打开新标签7 & &Actions actionOpenLinkInNewTab = new Actions(TestBase.driver);8   actionOpenLinkInNewTab.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).perform();9   //浏览器等待10秒,在这10秒内手动输入验证码并点击登录10   Thread.sleep(10 * 1000);11   //切换到新打开的标签12   String currentWindow = driver.getWindowHandle();13   Set&String& handles = driver.getWindowHandles();14   handles.remove(currentWindow);15   if (handles.size() & 0) {16 & & // 定位窗口17 & & driver.switchTo().window(handles.iterator().next());18   }19   //新标签中,系统自动登录网站20   driver.get(CommonBean.YICHEURL);21   Thread.sleep(5 * 1000);22 }
公益活动:
搜索风云榜
51Testing官方微信
51Testing官方微博
测试知识全知道laravel 搭建oauth2认证服务,可用于app登录等验证 - 简书
laravel 搭建oauth2认证服务,可用于app登录等验证
oauth2是一种验证方式, 这里不加以解释,不明白的小伙伴可以看一看阮一峰的文章-
按github上的教程搭建oauth2认证先进行composer安装然后配置,数据迁移。
选择一种验证方式oauth2-server-laravel 支持Client Credentials Grant,password等四种验证方式,可按照教程在config/oauth2.php下配置,例如选择password granttype,就可按照操作。
登录验证(这里使用密码方式)现在oauth_clients表中新建一条数据,然后每次登录验证时就发送参数client_id和client_secret; 在User.php修改验证的用户表protected $table="ims_mc_members";
在routes.php新建以下路由Route::post('oauth/access_token', function() {
return Response::json(Authorizer::issueAccessToken());
表单请求上面的路由,发送表单数据grant_typ = "password"
client_id = "your_client_id"
client_secret = "your_client_secret"
username = "you_"
password = "your_password",
注意2点,1.按照自己写的restapi格式发送表单,post或者get的话指定action就好,其他如delete,patch,put等就需要对应的在表单中加上一个隐藏的input如:_method = 'delete'
2.默认用邮箱登录以上就搭建完了,等等,如果我登录不用邮箱怎么办,如果我用了salt加密怎么办, 那往下看吧。
自定义验证规则
有时候可能不是用邮箱登录,或者验证方式与上面的不同,比如我的系统使用md5+salt验证,mobile和email都可登录,这时候就要修改默认验证方式了
修改app下PasswordGrantVerifier.php的verify.php方法
public function verify($username, $password)
//harry 修改验证 anasit
$credentials = [
'uniacid' =& $_REQUEST['uniacid'],
'password' =& $password,
if(strpos($username, '@')){
$credentials['email'] = $
$credentials['mobile'] = $
if (Auth::once($credentials)) {
return Auth::user()-&
因为之前会正则验证邮箱和手机号,所以就偷了个懒,用是否含有@来区分邮箱和手机号了。
这样就修改成mobile和email都可登录了哎,上面的uniacid是什么鬼?uniacid是因为在做微信开发,识别不同公众号,比如权限管理,管理员和用户都在一个user表中,一个人可以是管理员也可以是用户,他的账号密码可能相同,只是一个的role(角色)是user,一个是admin,这样你就可以在$credentials,加入一个键值对'role'=&'admin'或'role'=&'user'来做验证。但是接下来还要修改验证方式,验证方法在/vendor/laravel/frameword/src/Illuminate/Auth/EloquentUserProvider.php里面的validateCredentials方法可以自定义自己的验证逻辑md5,hash或者其他,我的修改为public function validateCredentials(UserContract $user, array $credentials)
$plain = $credentials['password'];
$salt =$user-&getAuthSalt();
$upwd = md5($plain.$salt.config('app.authkey'));
return $user-&getAuthPassword() == $
getAuthPassword是获取用户表中的password,但是我还要salt验证,就需要新建一个getAuthSalt()方法取得用户表中的salt,需要在\vendor\laravel\framework\src\Illuminate\Auth\Authenticatable.php,\vendor\laravel\framework\src\Illuminate\Auth\GenericUser.php,\vendor\laravel\framework\src\Illuminate\Contracts\Auth\Authenticatable.php,是不是好多文件不好找,其实在vendor文件下搜索一下getAuthPassword就基本知道要修改哪些文件了,修改好的是这样的\vendor\laravel\framework\src\Illuminate\Auth\Authenticatable.phppublic function getAuthSalt()
return $this-&
\vendor\laravel\framework\src\Illuminate\Auth\GenericUser.phppublic function getAuthSalt()
return $this-&attributes['salt'];
\vendor\laravel\framework\src\Illuminate\Contracts\Auth\Authenticatable.phppublic function getAuthPassword();
这样就万事大吉了,开心的写代码吧。
定义url获取access_token
在routers.php加入下面代码
Route::post('oauth/access_token', function() {
return Response::json(Authorizer::issueAccessToken());
给需要登录才能看到的资源(控制器)加入oauth2中间件
如routers.php加入下面代码, 访问购物车时需要access_token
Route::put('wechat/{uniacid}/anas_shop/cart', ['middleware' =& 'oauth', 'uses' =& 'Anas_shop\CartController@put']);
提交表单时需要加入
&input type="hidden" name="access_token" value="xxxxxxxxxxxx" &
获取当前access_token的用户id
使用Authorizer::getResourceOwnerId(); 方法即可获取,这里写成model供控制器调用
namespace A
//文件路径
use Illuminate\Support\Facades\A
use Illuminate\Database\Eloquent\M
use LucaDegasperi\OAuth2Server\Facades\A
class Functions extends Model {
protected static function memberinfo(){
$member_id = Authorizer::getResourceOwnerId(); 获取到的id
$member = DB::table('ims_mc_members')-&where('uid', $member_id)-&first();
更多oauth2的使用,去看文档吧
希望这篇文章能帮到你!OAuth2.0授权学习—2,账号密码授权 - 时间是把杀刀猪 - ITeye技术网站
博客分类:
对于没有服务端的第三方应用,使用账号密码授权也是一种方式(不过需要基于用户对这个应用足够信任的前提)
1,申请Client ID(填写应用ID号和密码,这里不需要填写回调网址)
http://localhost/oauth/server/examples/mongo/addclient.php
填写ID=50001,Secret=pwd提交,查询数据库如下
& db.clients.find()
{ "_id" : "50001", "pw" : "pwd", "redirect_uri" : "" }
2,直接使用用户账号,密码获取access token
发起请求如下
http://localhost/oauth/server/examples/mongo/token.php?
client_id=50001&
client_secret=pwd&
grant_type=password&
username=ciaos&
password=ciaospwd
"access_token":"1efd8b9a68de18fff899f",
"expires_in":3600,
"scope":null
查询数据库如下
& db.tokens.find()
{ "_id" : "1efd8b9a68de18fff899f", "client_id" : "50001", "expires" : , "scope" : null }
当然服务器端需要支持这种授权机制,我们需要在MongoOAuth中重载OAuth2这个类的相关函数如下:
* Overrides OAuth2::getSupportedGrantTypes().
protected function getSupportedGrantTypes() {
return array(
OAUTH2_GRANT_TYPE_AUTH_CODE,
OAUTH2_GRANT_TYPE_USER_CREDENTIALS
以及判断用户账号密码的逻辑
* Implements OAuth2::checkClientCredentials().
protected function checkUserCredentials($client_id, $username, $password) {
//check whether account information is correct
if(!($username == "ciaos" && $password == "ciaospwd")){
//check whether it's a valid client
$client = $this-&db-&clients-&findOne(array("_id" =& $client_id));
return $client !== NULL;
这样我们就可以用第二步获取到的access token直接访问受限资源了,访问方法和网站授权一样,不再介绍。
浏览: 133664 次
来自: 深圳
请问 var zTool = require(&./ ...
代码中的io.sockets.emit(socket.id,d ...OAuth2的学习总结 - 互联网当前位置:& &&&OAuth2的学习总结OAuth2的学习总结&&网友分享于:&&浏览:94次OAuth2的学习小结
OAUTH2核心参数说明
grant_type参数说明表格:
grant_type
authorization_code
标准的Server授权模式
基于用户密码的授权模式
client_credentials
基于APP密钥的授权模式
refresh_token
刷新accessToken
response_type参数说明表格:
response_type
标准的Server授权模式响应模式
脚本的授权响应模式,直接返回token,需要对回调进行校验
OAUTH2各种请求流程
Authorization Code(标准请求流程,必须实现)
标准的的Server授权模式,与目前开放平台的Session机制很像。
APP首先发送获取code请求
GET /authorize?response_type=code&client_id=s6BhdRkqt3&
redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
容器返回code
HTTP/1.1 302 Found
Location: /cb?code=i1WsRn1uB1
APP根据code发送获取token请求
POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&client_id=s6BhdRkqt3&
client_secret=gX1fBat3bV&code=i1WsRn1uB1&
redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
容器直接返回token
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
"access_token":"SlAV32hkKG",
"token_type":"example",
"expires_in":3600,
"refresh_token":"8xLOxBtZp8",
"example_parameter":"example-value"
Implicit Grant(直接发放模式)
适用于运行于浏览器中的脚本应用,需要校验callback地址,而且只返回该应用注册的回调地址
APP直接请求token
GET /authorize?response_type=token&client_id=s6BhdRkqt3&
redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
容器通过重定向返回token
HTTP/1.1 302 Found
Location: /rd#access_token=FJQbwq9&
token_type=example&expires_in=3600
Resource Owner Password Credentials (基于用户名与密码模式)
称之为用户名密码模式,需要提供终端用户的用户名和密码,适用于比如操作系统或者高权限的应用。
APP直接带上用户名和密码请求
POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=password&client_id=s6BhdRkqt3&
client_secret=47HDu8s&username=johndoe&password=A3ddj3w
容器直接返回token
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
"access_token":"SlAV32hkKG",
"token_type":"example",
"expires_in":3600,
"refresh_token":"8xLOxBtZp8",
"example_parameter":"example-value"
Client Credentials
基于APP的密钥直接进行授权,APP的权限非常大,慎用。这个模式可以考虑用于目前我们不需要弹出授权的特殊应用,如淘江湖,前端插件等。
APP直接根据客户端的密码来请求
POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id=s6BhdRkqt3&
client_secret=47HDu8s
容器直接返回token
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
"access_token":"SlAV32hkKG",
"token_type":"example",
"expires_in":3600,
"refresh_token":"8xLOxBtZp8",
"example_parameter":"example-value"
优先考虑实现的流程
Authorization Code为我们需要优先支持的流程,很多开源的OAUTH实现都是优先实现了该授权流程。ETAO的B2C网站会用这个流程与开放平台交互。
目前OAUTH 2有比较多的开源实现,其中比较好的开源实现是OAuth for Spring Security,大家可以参考http://static.springsource.org/spring-security/oauth/tutorial.html这个网址去具体了解。有兴趣的同学可以去这个网址去下载其源代码看看http://maven.springframework.org/milestone/org/springframework/security/oauth/spring-security-oauth/1.0.0.M2/spring-security-oauth-1.0.0.M2-sources.jar ,容器主要关注下面几个类:org.springframework.security.oauth2.provider.OAuth2AuthorizationFilter
org.springframework.security.oauth2.provider. DefaultOAuth2GrantManager
org.springframework.security.oauth2.provider.verification.VerificationCodeFilter
第一个和第二个类为参数校验和参数解析,第三个类为响应生成的类。
TIP主要关注下面的类:
org.springframework.security.oauth2.provider.OAuth2ProtectedResourceFilter
这个类主要实现了对AccessToken的校验
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有

我要回帖

更多关于 oauth验证接口 的文章

 

随机推荐