我正在通过Laravel 5上的AJAX上传文件.除了一件事,我几乎所有工作都在工作.
当我尝试上传一个太大的文件时(比我更大upload_max_filesize,post_max_size我得到了一个TokenMismatchException).
但是,这是预料之中的,因为我知道如果超出这些限制,我的输入将为空.空输入,意味着没有_token收到,因此负责验证CSRF令牌的中间件为什么会大惊小怪.
然而,我的问题不是抛出这个异常,而是渲染它的方式.当Laravel捕获到这个异常时,它正在为通用的Whoops页面吐出HTML(由于我处于调试模式,因此加载了堆栈跟踪).
处理此异常的最佳方法是什么,以便通过AJAX返回JSON(或者在请求JSON时),同时保持默认行为?
编辑:无论抛出什么异常,这似乎都会发生.我刚尝试通过AJAX(数据类型:JSON)向一个"页面"发出请求,该页面在尝试获取404时不存在,同样的事情发生 - 返回HTML,没有JSON友好.
我正在处理一个表格,用户可以更新他们的出生日期.的形式给出用户3的单独字段day,month和year.当然,在服务器端,我想将这3个独立的字段视为一个值即yyyy-mm-dd.
因此,在验证和更新我的数据库之前,我想date_of_birth通过连接来改变表单请求以创建字段year,month并day使用-字符来创建我需要的日期格式(并且可能取消设置原始的3个字段).
使用我的控制器手动实现此功能不是问题.我可以简单地抓取输入,将字段连接在一起,-然后取消设置.然后,我可以在传递给处理命令之前手动验证.
但是,我更愿意使用a FormRequest来处理验证并将其注入到我的控制器方法中.因此,我需要一种在执行验证之前实际修改表单请求的方法.
我确实找到了类似的以下问题:Laravel 5 Request - 改变数据
它建议覆盖all表单请求上的方法,以包含在验证之前操作数据的逻辑.
<?php namespace App\Http\Requests;
class UpdateSettingsRequest extends Request {
public function authorize()
{
return true;
}
public function rules()
{
return [];
}
public function all()
{
$data = parent::all();
$data['date_of_birth'] = 'test';
return $data;
}
Run Code Online (Sandbox Code Playgroud)
这对于验证来说都很好,但是覆盖该all方法实际上并不会修改表单请求对象上的数据.因此,在执行命令时,表单请求包含原始未修改的数据.除非我使用现在重写的all方法来拉出数据.
我正在寻找一种更具体的方法来修改我的表单请求中的数据,不需要调用特定的方法.
干杯
我在我的项目中为我的一个模型设置了一些路径/模型绑定,并且工作得很好.我能够在路径路径中使用绑定,并接受我的模型实例作为控制器中相关方法的参数.
现在我正在尝试使用这个模型,所以我在我的控制器中创建了一个接受表单请求的方法,以便我可以进行一些验证.
public function edit(EditBrandRequest $request, Brand $brand)
{
// ...
Run Code Online (Sandbox Code Playgroud)
我的模型的每个不同实例都可以以不同方式进行验证,因此我需要能够使用模型的实例来构建一组自定义验证规则.
有没有办法获取模型的实例,从表单请求注入控制器?
我尝试在Form Request的构造函数中对模型实例进行类型提示
class EditBrandRequest extends Request
{
public function __construct(Brand $brand)
{
dd($brand);
}
Run Code Online (Sandbox Code Playgroud)
我也尝试在Form Request的rules()方法中对模型实例进行类型提示.
class EditBrandRequest extends Request
{
// ...
public function rules(Brand $brand)
{
dd($brand);
Run Code Online (Sandbox Code Playgroud)
在这两个实例中,我都提供了模型的空/新实例,而不是我期望的实例.
当然,我总是可以通过不打扰表单请求来解决这个问题,只需在控制器中生成规则并手动验证 - 但如果可能的话,我宁愿选择Laravel方式.
谢谢
在Laravel中指定存在的验证规则时,是否有一种引用另一个字段的方法?我希望能够说输入a必须存在于表a中,输入b必须存在于表b中,并且表b中列x的值必须等于输入a.
最好用例子解释:
public $rules = array(
'game_id' => 'required|exists:games,id',
'team1_id' => 'required|exists:teams,id,game_id,<game_id input value here>',
'team2_id' => 'required|exists:teams,id,game_id,<game_id input value here>'
);
Run Code Online (Sandbox Code Playgroud)
因此,根据我的验证规则,我希望能够确保:
game_id存在于games表(id字段)中team1_id存在于teams表(id字段)中,game_id列(在teams表中)必须等于game_id输入的值.team2_id所以,如果在我的形式,我进入1了game_id,我希望能够确保两者的球队表中的记录team1_id,并team2_id具有价值1的game_id.
我希望这是有道理的.
谢谢
我的Spring Boot项目中有一个相当基本的设置。我正在尝试设置OAuth2来保护我的API,但是我遇到了/oauth/token端点问题。向我的/oauth/token端点发出POST或GET请求将导致以下响应(带有401 Unauthorized状态代码):
{
"timestamp": "2018-09-17T16:46:59.961+0000",
"status": 401,
"error": "Unauthorized",
"message": "Unauthorized",
"path": "/oauth/token"
}
Run Code Online (Sandbox Code Playgroud)
这是我的授权服务器配置。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.approval.UserApprovalHandler;
import org.springframework.security.oauth2.provider.token.TokenStore;
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private TokenStore tokenStore;
@Autowired
private UserApprovalHandler userApprovalHandler;
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory() …Run Code Online (Sandbox Code Playgroud) java spring spring-security spring-boot spring-security-oauth2
我一直有很多与Mod Security有关的问题.我正忙于为工作中的项目编写CMS,在开发编辑某个数据库记录的页面时,我不断收到403错误.经过几个小时的敲打我的桌子后,调整了一些代码,我最后只是更改了我的表单发布到的脚本,包含一个简单的echo "test";.即使提交到这个简单的页面也会引发403错误.我搞砸了我的表格,我最终发现,如果我减少了数据量,我发布的表单提交得很好(特别是我减少了textarea中的文本数量).
检查完日志后(是的,这不是我做的第一件事 - 感叹)我注意到我从ModSecurity那里收到了很多错误,例如:
[Mon Aug 12 16:34:45 2013] [error] [client XX.XXX.XXX.XXX] ModSecurity: Failed to access DBM file "/etc/httpd/logs//global": Permission denied [hostname "XXXXXXX.XXX"] [uri "/admin/index.php"] [unique_id "UgkAlW1shFcAAHTMK80AAAAF"]
[Mon Aug 12 16:34:45 2013] [error] [client XX.XXX.XXX.XXX] ModSecurity: Failed to access DBM file "/etc/httpd/logs//ip": Permission denied [hostname "XXXXXXX.XXX"] [uri "/admin/index.php"] [unique_id "UgkAlW1shFcAAHTMK80AAAAF"]
[Mon Aug 12 17:11:33 2013] [error] [client XX.XXX.XXX.XXX] ModSecurity: Rule execution error - PCRE limits exceeded (-8): (null). [hostname "XXXXXXX.XXX"] [uri "/admin/index.php"] [unique_id "UgkJNW1shFcAAHXUMHkAAAAH"]
[Mon Aug …Run Code Online (Sandbox Code Playgroud) 我composer update今天早上刚刚完成了我的项目,这反过来更新了我的 Laravel 项目中的一些依赖项。自从这次更新以来,我特别注意到一件事已经停止工作。
在我的一个命令处理程序中,我注入了一个Filesystem依赖项并按以下方式使用它:
use Illuminate\Contracts\Filesystem\Filesystem; // NOTE: This is an interface
...
class MyCommandHandler {
protected $filesystem;
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}
public function handle()
{
if ($this->filesystem->exists('/directory/that/does/not/exist'))
{
// Do something
}
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到,当使用不存在的目录调用exists注入$filesystem对象上的方法时,条件就好像该目录确实存在一样传递,并且我最终在我的代码中进一步出现问题。
我曾经get_class()输出$filesystem对象的类,结果是Illuminate\Filesystem\FilesystemAdapter. 进一步调查我发现我的文件系统操作最终由League\Flysystem包处理,而以前它们由Illuminate\Filesystem\Filesystem.
我不完全确定为什么对 Flysystem 的更改破坏了我的代码,但我有几个关于 Laravel 中的依赖注入的问题。
它是更好地typehint的接口,让Laravel决心从IoC容器(如我在这里所做的)或者是更好的(在我的情况下注入所以typehint具体类Illuminate\Filesystem\Filesystem)?
interface -> implementation 的绑定在哪里定义?另外,可以在不更改核心代码的情况下修改它们吗?
干杯
编辑
控制器中的一些测试代码。输出yes|no …
我希望将个性化的批处理电子邮件发送给大量用户。我的意思是,我想设置一个模板电子邮件,然后在发送之前将每个用户的信息注入其中。
当然,这可以容易地与Laravel通过用户数据循环,并使用邮件程序(或者实现Mail门面)方法(如send,raw,queue等等):
foreach ($users as $user) {
$data = ['user' => $user];
$this->mailer->queue($views, $data, function($message) use($user) {
$message->to($user->email, $user->name);
});
}
Run Code Online (Sandbox Code Playgroud)
但是,考虑到我想发送的电子邮件数量,这对于我的需求来说太慢了。经过一些研究,我发现Mailgun支持使用其API发送个性化批处理电子邮件。从他们的网站:
批量发送
通过一个API调用,您最多可以发送1000个完全个性化的电子邮件。
Mailgun将正确组装MIME消息,并将电子邮件分别发送给每个用户。这使得发送大量电子邮件的速度大大加快,并且减少了资源消耗。
当然,我可以直接使用Mailgun的API或使用任何可用的SDK来愉快地实现此功能,但只想先检查一下Laravel是否支持它即可。
我有一个相当基本的 Spring Boot 设置,我已经安装了 Spring Security,我已经成功设置了 OAuth2 来保护我的 API。
几天前我遇到了一些麻烦,问(并回答)了一个关于达到我的/oauth/token终点的问题。我很快发现问题在于我试图在我的POST请求正文中发送我的客户端凭据,但在 Spring Security 中配置了令牌端点,以通过 HTTP 基本身份验证接受客户端凭据(client_id和secret)。
我使用 OAuth2 API 的大部分经验都涉及在POST请求正文中发送客户端凭据,我想知道是否可以将 Spring Security 配置为以相同的方式运行?
我尝试了一些不同的事情但没有成功,比如设置以下配置选项,但我觉得这可能只在配置 OAuth2 客户端时使用:
security.oauth2.client.clientAuthenticationScheme=form
Run Code Online (Sandbox Code Playgroud)
这是我的授权服务器配置。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.approval.UserApprovalHandler;
import org.springframework.security.oauth2.provider.token.TokenStore;
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private TokenStore tokenStore;
@Autowired
private UserApprovalHandler userApprovalHandler;
@Autowired …Run Code Online (Sandbox Code Playgroud) 我希望能够在我的应用程序中为不同的域使用不同的路由.我想根据域名是否采取不同的行动
mysite.com/somethingsubdomain.mysite.com/somethinganotherdomain.com我这样解决了这个问题:
// Match my own domain
Route::group(['domain' => 'mysite.com'], function()
{
Route::any('/', function()
{
return 'My own domain';
});
});
// Match a subdomain of my domain
Route::group(['domain' => '{subdomain}.mysite.com'], function()
{
Route::any('/', function($subdomain)
{
return 'Subdomain ' . $subdomain;
});
});
// Match any other domains
Route::group(['domain' => '{domain}'], function()
{
Route::any('/', function()
{
return 'Full domain ';// . $domain;
});
});
Run Code Online (Sandbox Code Playgroud)
前两组完美运作.访问mysite.com显示My own domain和访问按预期subdomain.mysite.com显示Subdomain …