我开始学习基于令牌的身份验证,我正在尝试学习如何在Laravel 5中实现它.我遇到了两种非常流行的技术,但我很困惑,因为我对这两种技术都不熟悉.
Medium的这篇文章说我应该选择lucadegasperi/oauth2-server-laravel,我肯定这是一个非常受欢迎的社区包,根据Github上的明星数量以及引导我的参考数量来判断.这个应该可以帮助我实现OAuth.
Scotch.io上的另一篇文章鼓励我使用tymondesigns/jwt-auth,这也是非常受欢迎的,再次根据Github上的明星数量来判断.
在这一点上,我优先考虑使用哪一个,因为我是一个新手开发人员,我没有与他们中的任何一个合作.
任何人都可以向我指出每个人的利弊是什么以及我应该实施哪一个?我的项目类型是否也决定了我应该使用哪种类型?如何?
此外,如果你正在争论我应该选择另一个,你还能指出可以帮助我从中开始的好资源.除了这两个链接,我当然是自己提供的.
我正在ASP.NET Core 1.0中创建一个REST api.我正在使用Swagger进行测试,但现在我为某些路线添加了JWT授权.(带UseJwtBearerAuthentication)
是否可以修改Swagger请求的标头,以便[Authorize]可以测试具有该属性的路由?
我目前在System.IdentityModels.Tokens命名空间中使用JwtSecurityToken类.我使用以下方法创建令牌:
DateTime expires = DateTime.UtcNow.AddSeconds(10);
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
var genericIdentity = new System.Security.Principal.GenericIdentity(username, "TokenAuth");
ClaimsIdentity identity = new ClaimsIdentity(claims);
string secret = ConfigurationManager.AppSettings["jwtSecret"].ToString();
var securityKey = new InMemorySymmetricSecurityKey(Encoding.Default.GetBytes(secret));
var signingCreds = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature, SecurityAlgorithms.HmacSha256Signature);
var securityToken = handler.CreateToken(
issuer: issuer,
audience: ConfigurationManager.AppSettings["UiUrl"].ToString(),
signingCredentials: signingCreds,
subject: identity,
expires: expires,
notBefore: DateTime.UtcNow
);
return handler.WriteToken(securityToken);
Run Code Online (Sandbox Code Playgroud)
由于某种原因,即使将expires设置为当前时间之后的10秒,它也不会在令牌被验证之前实际抛出异常,直到大约5分钟.看到这个之后,我想也许最短的过期时间是5分钟,所以我将过期时间设置为:
DateTime.UtcNow.AddMinutes(5);
Run Code Online (Sandbox Code Playgroud)
然后它在10分钟到期,但异常消息表明过期时间设置为应该是的(用户登录后5分钟),当它显示异常中的当前时间时,它是5分钟后到期时间.因此,它似乎知道什么时候它应该过期,但它实际上并没有在过期时间后5分钟抛出异常.然后,由于令牌似乎在我将其设置为过期的任何时间添加5分钟,我将过期时间设置为:
DateTime.UtcNow.AddMinutes(-5).AddSecond(10);
Run Code Online (Sandbox Code Playgroud)
我测试了这个,到目前为止它还没有过期(超过十分钟后).有人可以解释为什么会发生这种情况以及我做错了什么?此外,如果您看到我提供的代码的任何其他内容,我将不胜感激,因为我不熟悉使用JWT和此库.
先感谢您
我已经实现了一个基本的身份验证系统,Spring Boot,Spring Security,OAUTH2和JWT作为auth令牌.它工作正常,但我在想是否有意义将JWT存储在数据库中并检查每当有人使用它进行经过身份验证的请求时是否存在令牌?我特别想到以下场景:用户在移动设备中进行了身份验证,但是他们失去了它,因此他们想要取消对该设备的授权.然后,他们将能够发出一个操作,清除发给他们用户ID的令牌,并取消授权分配给他的所有令牌.还有其他方法吗?我认为这是错误的还是过于复杂的事情?
这是为了保护将从移动APP调用的REST API.
我正在做一个学习 Laravel 的项目,我看到了一些教程可以用jwt auth在这个项目上。它运行良好,但现在并不总是显示错误,但我不知道为什么。就是这个:
传递给 Lcobucci\JWT\Signer\Hmac::doVerify() 的参数 3 必须是 Lcobucci\JWT\Signer\Key 的实例,给定 null,在 C:\xampp\htdocs\inmobiliaria\vendor\lcobucci\jwt\src 中调用\Signer\BaseSigner.php 第 42 行
有时它有效,但不是另一个。所以我不知道我能做什么。我试过
但问题仍然存在,有时我会从服务器(200)那里得到很好的答案,但大部分时间都很糟糕(500)。
截图


我的第一个堆栈溢出问题 - 就在这里!
我已经编码了一个 Firebase JWT 令牌来验证我的 PHP Slim API 中的“买家”。它正确编码并在 Postman 中生成 JWT 令牌,
但是当我尝试使用 JWT 进行承载身份验证来访问routes.php 文件中的一组受保护路由时,我收到:
{ "status": "\"kid\" empty, unable to lookup correct key" }
这是我的generateJWT() 函数: - 在Buyer.php 类中。
{
$buyer = $buyer = self::findOrFail($id);
if (!$buyer) {
return false;
}
$key = self::JWT_KEY;
$expiration = time() + self::JWT_EXPIRE;
$issuer = 'Lab03_I425.com';
$token = [
'iss' => $issuer,
'exp' => $expiration,
'isa' => time(),
'data' => [
'uid' => $id,
'name' => …Run Code Online (Sandbox Code Playgroud) 所以我从Dave Syer的这个例子中得到了以下授权服务器
@SpringBootApplication
public class AuthserverApplication {
public static void main(String[] args) {
SpringApplication.run(AuthserverApplication.class, args);
}
/* added later
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
protected static class MyWebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http //.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll();
}
}*/
@Configuration
@EnableAuthorizationServer
protected static class OAuth2AuthorizationConfig extends
AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
KeyPair keyPair = new KeyStoreKeyFactory(
new ClassPathResource("keystore.jks"), "foobar".toCharArray())
.getKeyPair("test");
converter.setKeyPair(keyPair);
return converter; …Run Code Online (Sandbox Code Playgroud) 我正在尝试调用服务,它可以在DHC中运行,但是当我尝试调用我的angular 2项目时,它会崩溃,方法请求是POST,从具有电子邮件和密码的正文接收对象,以及响应是一个令牌,我将在整个项目中用于授权.
这是我的代码.
import { Injectable } from '@angular/core';
import { Http, Response, Headers, Request ,RequestOptions ,RequestMethod } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
@Injectable()
export class LoginService {
constructor(private http:Http) {
}
getToken(){
var baseUrl = "xxx.xxx.x.xxx:xxxx/project/v1/admin/login";
var headers = new Headers();
headers.append("Content-Type", 'application/json');
var options = new RequestOptions({ headers: headers });
var objeto = {'email': 'xxxxxx', 'pass':'xxxx' }
var body2 = JSON.stringify(objeto);
var xhr = new XMLHttpRequest();
return this.http
.post(baseUrl, body2, options)
.map((response: Response) => response.json()) …Run Code Online (Sandbox Code Playgroud) 如何在springdoc-openapi-ui (OpenAPI 3.0 /swagger-ui.html) 中为不记名令牌认证启用“授权”按钮,例如 JWT。
Spring@Controller和@Configuration类需要添加哪些注解?
Cookies不是通过 发送到服务器的getServerSideProps,下面是前端的代码:
export async function getServerSideProps() {
const res = await axios.get("http://localhost:5000/api/auth", {withCredentials: true});
const data = await res.data;
return { props: { data } }
}
Run Code Online (Sandbox Code Playgroud)
在服务器上,我有一个检查访问 JWT 令牌的策略。
export class JwtStrategy extends PassportStrategy(Strategy, "jwt") {
constructor() {
super({
ignoreExpiration: false,
secretOrKey: "secret",
jwtFromRequest: ExtractJwt.fromExtractors([
(request: Request) => {
console.log(request.cookies) // [Object: null prototype] {}
let data = request.cookies['access'];
return data;
}
]),
});
}
async validate(payload: any){
return payload;
}
}
Run Code Online (Sandbox Code Playgroud)
也就是说,当我通过getServerSidePropscookie 发送请求时,不会到达服务器,尽管如果我发送,例如通过useEffect …