你好我试图在我的webapp上实现passport-jwt。但它不会工作我已经看过几个相同的帖子并尝试了他们所做的但是在我的情况下它没有用。
这里有一些Authenticating node API with passport-jwt
passport local strategy not getting called
我已经尝试过这些但它没有奏效。 这是我的passport.js上的代码
var config = require('./config');
var JwtStrategy = require('passport-jwt').Strategy,
ExtractJwt = require('passport-jwt').ExtractJwt;
module.exports =
function(passport){
var opts ={};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = config.secret;
passport.use(new JwtStrategy(opts,function(jwt_payload,done){
console.log('HERE');
console.log(jwt_payload);
User.findOne({id:jwt_payload._id},function(err,user){
if(err)
return done(err,false);
if(user)
return done(null,user);
else
return done(null,false);
})
}));
};
它甚至无法到达console.log('HERE');
这就是我生成令牌的方式
router.post('/login',function(req,res,next){
var username = req.body.username;
var password = req.body.password;
User.findUser(username,function(err,user){
if(err) return res.json({success:false,msg:"Error"});
if(!user) return res.json({success:false,msg:"No Such user"});
//res.json(user);
console.log(user);
if(user){
User.comparePass(password,user.password,function(err,IsMatched){
if(err) return res.json({success:false,msg:'error});
if(!IsMatched) return res.json({success:false,msg:"wrong password"});
if(IsMatched){
var token = jwt.sign(user.toObject(),config.secret,{expiresIn:604800});
res.json({success:true,token:'JWT '+ token,
user:{
name:user.name,
username:user.username,
password:user.password,
email:user.email,
contact:user.contact,
address:user.address
}
});
}
});
}
});
});
以下是我试图访问的内容
router.get('/profile',passport.authenticate('jwt',{session:false}), function(req, res, next) {
res.json({user:req.user});
});
谢谢!
答案 0 :(得分:1)
好的,我已经弄清楚了。
public void setConfigurationSyncUser(SyncUser user) {
String user1RealmUrl = "realm://localhost:9080/~/default";
class SyncErrorHandler implements SyncSession.ErrorHandler {
@Override
public void onError(SyncSession session, ObjectServerError error) {
fail("Realm unexpected error: " + error);
}
@Override
public int hashCode() {
return SyncErrorHandler.class.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof SyncErrorHandler;
}
};
SyncConfiguration config = new SyncConfiguration.Builder(user, user1RealmUrl).
errorHandler(new SyncErrorHandler())
.build();
realm = Realm.getInstance(config);
}
而不是
public class DropDown_Settings {
static String baseURL = "https://google.com";
static WebDriver driver;
public static void main(String[] args) throws InterruptedException
{
driver= new FirefoxDriver();
driver.get(baseURL);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='fsettl']")).click();
driver.findElement(By.xpath(".//*[@id='advsl']/a[text()='Advanced
search']")).click();
driver.findElement(By.cssSelector("#lr_button")).click();
WebElement dropDown=driver.findElement(By.id("lr_menu"));
List<WebElement> options=dropDown.findElements(By.tagName("li"));
System.out.println(options.size());
for(WebElement value:options){
if(value.getText().equals("Hindi")){
System.out.println(value.getText());
value.click();
break;
}
}
}
}