<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Symfony中文教程 &#187; UserInterface</title>
	<atom:link href="http://www.newlifeclan.com/symfony/archives/tag/userinterface/feed" rel="self" type="application/rss+xml" />
	<link>http://www.newlifeclan.com/symfony</link>
	<description>站在巨人肩膀上的phpweb框架</description>
	<lastBuildDate>Fri, 12 Dec 2025 00:58:27 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>KU案例2之08拒绝访问：Entity Security</title>
		<link>http://www.newlifeclan.com/symfony/archives/425</link>
		<comments>http://www.newlifeclan.com/symfony/archives/425#comments</comments>
		<pubDate>Mon, 06 Apr 2015 09:28:52 +0000</pubDate>
		<dc:creator><![CDATA[napoleon]]></dc:creator>
				<category><![CDATA[实战教程]]></category>
		<category><![CDATA[UserInterface]]></category>

		<guid isPermaLink="false">http://www.newlifeclan.com/symfony/?p=425</guid>
		<description><![CDATA[<p>跟着我，“我们真的很棒”。和我们的security系统一样的棒。让我们跟上这个速度，从数据库加载用户替代sec [&#8230;]</p>
<p><a rel="nofollow" href="http://www.newlifeclan.com/symfony/archives/425">KU案例2之08拒绝访问：Entity Security</a>，首发于<a rel="nofollow" href="http://www.newlifeclan.com/symfony">Symfony中文教程</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>跟着我，“我们真的很棒”。和我们的security系统一样的棒。让我们跟上这个速度，从数据库加载用户替代security.yml中硬编码用户。</p>
<p>我们要做的类似于一个开源的fosuserbundle。我们要自己打造这一切，以便我们明白他是如何工作的。<span id="more-425"></span></p>
<p>生成User Entity</p>
<div>ok，忘记这个security！不是开玩笑！仅仅想我们要存储一些用户信息到数据库。要做到这点，我们需要创建user entity类。</div>
<div>这听起来工作量很大，但是我们只需要使用app/console <tt>doctrine:generate:entity</tt> <tt>命令</tt></div>
<div>
<pre class="crayon-plain-tag">php app/console doctrine:generate:entity</pre>
</div>
<div><span style="font-family: monospace">使用userbundle:user这个短名称，记住，doctrine在entity中使用短语法。</span></div>
<div>
<div><span style="font-family: monospace">这个类我们需要两个字段：</span></div>
<div><span style="font-family: monospace">   username和password</span></div>
<div>当然，在命令行提示中选择yes去生成repository类。<br />
这个命令行替我们完成了代码的创建，我们应该在UserBundle的Entity目录下有一个User类，让我们打开它并改写表名称为yada_user：</div>
<div>
<pre class="crayon-plain-tag">// src/Yoda/UserBundle/Entity/User.php
namespace Yoda\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="yoda_user")
 * @ORM\Entity(repositoryClass="Yoda\UserBundle\Entity\UserRepository")
 */
class User
{
    // ... the generated properties and getter/setter functions
}</pre></p>
<h2> 实现UserInterface</h2>
<p>现在我们看到的只是一个普通的doctrine实体与security并无关系。但是，我们的目标是在登录的时候从数据表加载用户。要想实现此功能，第一步是创建你的类实现UserInterface接口：</p><pre class="crayon-plain-tag">// src/Yoda/UserBundle/Entity/User.php
// ...

use Symfony\Component\Security\Core\User\UserInterface;

class User implements UserInterface
{
    // ...
}</pre><p>这个接口需要5个方法！我们已经有两个了：getUsername()和getPassword():</p><pre class="crayon-plain-tag">// src/Yoda/UserBundle/Entity/User.php
// ...

public function getUsername()
{
    return $this-&gt;username;
}

public function getPassword()
{
    return $this-&gt;password;
}</pre><p>ok,让我们添加其他3个方法：</p>
<p>首先，getRoles()返回一个用户角色数组。现在我们就返回一个硬编码角色，ROLE_USER。</p><pre class="crayon-plain-tag">// src/Yoda/UserBundle/Entity/User.php
// ...

public function getRoles()
{
    return array('ROLE_USER');
}</pre><p>其次，添加eraseCredentials。该方法是空的。稍后我们将添加一些逻辑：</p><pre class="crayon-plain-tag">public function eraseCredentials()
{
    // blank for now
}</pre><p>最后，添加getSalt(),只是让他返回空即可：</p><pre class="crayon-plain-tag">public function getSalt()
{
    return null;
}</pre><p>我们会谈谈第二个方法：</p>
<p>现在，user类已经实现了UserInterface，自然symfony2的authentication系统将能够使用它。但在此之前，让我们添加yoda_user表到数据库，运行doctrine:schema:update命令：</p><pre class="crayon-plain-tag">php app/console doctrine:schema:update --force</pre><p></p>
<h2> 在security.yml中加载doctrine用户</h2>
<p>这个很重要。我们要告诉security系统要使用我们的entity类！</p>
<p>在security.yml，替换encoders为我们自己的user类，并设置他的值为bcrypt</p><pre class="crayon-plain-tag"># app/config/security.yml
security:
    encoders:
        Yoda\UserBundle\Entity\User: bcrypt
    # ...</pre><p>这是告诉symfony我们的user类中的password字段使用bcrypt算法加密。</p>
<h2>安装password_compat</h2>
<p>一个美中不足的问题，php5.5之前是不支持bcrypt的。所以，如果你使用php5.4或者更低版本，则需要通过Composer安装。打开你的终端，使用composer的require命令：</p><pre class="crayon-plain-tag">php composer.phar require ircmaxell/password-compat</pre><p>如果它要求版本，请使用～1.0.3版本。顺便说一下，这个require命令只是更新我们的composer.json，在里面添加了一个快捷方式。我们还需要更新composer：</p><pre class="crayon-plain-tag">"require": {
    "...": "..."
    "ircmaxell/password-compat": "~1.0.3"
},</pre><p></p>
<h2>使用entity Provider</h2>
<p>在security.yml ,移除仅有的providers，并用一个新的来替换：</p><pre class="crayon-plain-tag"># app/config/security.yml
security:
    # ...

    providers:
        our_database_users:
            entity: { class: UserBundle:User, property: username }</pre><p>现在只是发明了our_database_users，这个是随便定制的名称。但是这个entity键是一个特殊的内置provider，告诉security如何通过doctrine来加载用户。</p>
<p>是的，这是真的吗？让我们试试吧。</p>
<p>当你刷新，你发现又抱错了：</p><pre class="crayon-plain-tag">There is no user provider for user "Symfony\Component\Security\Core\User\User".</pre><p>即便我们刚从security.yml删除了他，我们还是使用登录的硬编码用户。。。不要惊慌，他只是一个一次性的错误，只要刷新他，就会消失的。</p>
<p>下一节，创建和保存用户</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>
</div>
<p><a rel="nofollow" href="http://www.newlifeclan.com/symfony/archives/425">KU案例2之08拒绝访问：Entity Security</a>，首发于<a rel="nofollow" href="http://www.newlifeclan.com/symfony">Symfony中文教程</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.newlifeclan.com/symfony/archives/425/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
