Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony]Remember me, Nie zapamiętuje zalogowania
PawelC
post 6.03.2018, 21:33:24
Post #1





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

Ostrzeżenie: (0%)
-----


Witam,
Mam następujący problem, loguje się do aplikacji przez formularz i jest wszystko ok. Dodałem opcję remember me zgodnie z dokumentacją http://symfony.com/doc/3.4/security/remember_me.html

Niestety po zamknięciu przeglądarki muszę logować się ponownie sad.gif

Security.yml
  1. security:
  2. encoders:
  3. AppBundle\Entity\User:
  4. algorithm: bcrypt
  5. providers:
  6. our_db_provider:
  7. entity:
  8. class: AppBundle:User
  9. property: phone
  10. firewalls:
  11. main:
  12. anonymous: ~
  13. logout_on_user_change: true
  14. remember_me:
  15. secret: '%kernel.secret%'
  16. lifetime: 604800 # 1 week in seconds
  17. path: /
  18. domain: ~
  19. always_remember_me: true
  20. form_login:
  21. login_path: login
  22. check_path: login
  23. default_target_path: /diary/list
  24. always_use_default_target_path: true
  25. csrf_token_generator: security.csrf.token_manager
  26.  
  27. logout: true
  28. access_control:


Entity użytkownika
  1. <?php
  2.  
  3. namespace AppBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  7. use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  8.  
  9. /**
  10.  * @ORM\Table(name="app_users")
  11.  * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
  12.  * @UniqueEntity(fields="email", message="Email already taken")
  13.  * @UniqueEntity(fields="username", message="Username already taken")
  14.  * @UniqueEntity(fields="phone", message="Phone already taken")
  15.  */
  16. class User implements AdvancedUserInterface, \Serializable
  17. {
  18. /**
  19.   * @ORM\Column(type="integer")
  20.   * @ORM\Id
  21.   * @ORM\GeneratedValue(strategy="AUTO")
  22.   */
  23. private $id;
  24.  
  25. /**
  26.   * @ORM\Column(type="string", length=25, unique=true)
  27.   */
  28. private $username;
  29.  
  30. /**
  31.   * @ORM\Column(type="string", length=25, unique=true)
  32.   */
  33. private $phone;
  34.  
  35. /**
  36.   * @ORM\Column(type="string", length=25)
  37.   */
  38. private $role;
  39.  
  40. /**
  41.   * @ORM\Column(type="string", length=64)
  42.   */
  43. private $password;
  44.  
  45. /**
  46.   * @ORM\Column(type="string", length=60, unique=true)
  47.   */
  48. private $email;
  49.  
  50. /**
  51.   * @ORM\Column(name="is_active", type="boolean")
  52.   */
  53. private $isActive;
  54.  
  55. public function __construct()
  56. {
  57. $this->isActive = true;
  58. }
  59.  
  60. public function getUsername()
  61. {
  62. return $this->username;
  63. }
  64.  
  65. /**
  66.   * Set username
  67.   *
  68.   * @param string $username
  69.   *
  70.   * @return User
  71.   */
  72. public function setUsername($username)
  73. {
  74. $this->username = $username;
  75.  
  76. return $this;
  77. }
  78.  
  79. public function getSalt()
  80. {
  81. return null;
  82. }
  83.  
  84. public function getPassword()
  85. {
  86. return $this->password;
  87. }
  88.  
  89. /**
  90.   * Set password
  91.   *
  92.   * @param string $password
  93.   *
  94.   * @return User
  95.   */
  96. public function setPassword($password)
  97. {
  98. $this->password = $password;
  99.  
  100. return $this;
  101. }
  102.  
  103. public function getRoles()
  104. {
  105. return array($this->role);
  106. }
  107.  
  108. public function eraseCredentials()
  109. {
  110. }
  111.  
  112. public function isAccountNonExpired()
  113. {
  114. return true;
  115. }
  116.  
  117. public function isAccountNonLocked()
  118. {
  119. return true;
  120. }
  121.  
  122. public function isCredentialsNonExpired()
  123. {
  124. return true;
  125. }
  126.  
  127. public function isEnabled()
  128. {
  129. return $this->isActive;
  130. }
  131.  
  132. /**
  133.   * @return mixed
  134.   */
  135. public function getPhone()
  136. {
  137. return $this->phone;
  138. }
  139.  
  140. /**
  141.   * @param mixed $phone
  142.   */
  143. public function setPhone($phone)
  144. {
  145. $this->phone = $phone;
  146. }
  147.  
  148. /** @see \Serializable::serialize() */
  149. public function serialize()
  150. {
  151. return serialize(array(
  152. $this->id,
  153. $this->username,
  154. $this->password,
  155. $this->phone,
  156. $this->isActive,
  157. ));
  158. }
  159.  
  160. /** @see \Serializable::unserialize() */
  161. public function unserialize($serialized)
  162. {
  163. list (
  164. $this->id,
  165. $this->username,
  166. $this->password,
  167. $this->phone,
  168. $this->isActive,
  169. ) = unserialize($serialized);
  170. }
  171.  
  172. /**
  173.   * Get id
  174.   *
  175.   * @return integer
  176.   */
  177. public function getId()
  178. {
  179. return $this->id;
  180. }
  181.  
  182. /**
  183.   * Get email
  184.   *
  185.   * @return string
  186.   */
  187. public function getEmail()
  188. {
  189. return $this->email;
  190. }
  191.  
  192. /**
  193.   * Set email
  194.   *
  195.   * @param string $email
  196.   *
  197.   * @return User
  198.   */
  199. public function setEmail($email)
  200. {
  201. $this->email = $email;
  202.  
  203. return $this;
  204. }
  205.  
  206. /**
  207.   * Get isActive
  208.   *
  209.   * @return boolean
  210.   */
  211. public function getIsActive()
  212. {
  213. return $this->isActive;
  214. }
  215.  
  216. /**
  217.   * Set isActive
  218.   *
  219.   * @param boolean $isActive
  220.   *
  221.   * @return User
  222.   */
  223. public function setIsActive($isActive)
  224. {
  225. $this->isActive = $isActive;
  226.  
  227. return $this;
  228. }
  229.  
  230. /**
  231.   * Set role
  232.   *
  233.   * @param string $role
  234.   *
  235.   * @return User
  236.   */
  237. public function setRole($role)
  238. {
  239. $this->role = $role;
  240.  
  241. return $this;
  242. }
  243.  
  244. /**
  245.   * Get role
  246.   *
  247.   * @return string
  248.   */
  249. public function getRole()
  250. {
  251. return $this->role;
  252. }
  253. }


Kontroler logowania
  1. <?php
  2.  
  3. namespace AppBundle\Controller;
  4.  
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  6. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8.  
  9. class SecurityController extends Controller
  10. {
  11. /**
  12.   * @Route("/login", name="login")
  13.   */
  14. public function loginAction(AuthenticationUtils $authUtils)
  15. {
  16. $error = $authUtils->getLastAuthenticationError();
  17.  
  18. return $this->render('@App/Security/login.html.twig', array('error' => $error));
  19. }
  20.  
  21. /**
  22.   * @Route("/logout")
  23.   */
  24. public function logoutAction()
  25. {
  26.  
  27. }
  28.  
  29. }


[b]Formularz logowania


Formularz logowania
  1. {% extends 'base.html.twig' %}
  2. {% block body %}
  3.  
  4.  
  5. <form action="{{ path('login') }}" method="post">
  6. <div class="login">
  7.  
  8. {% if error %}
  9. <div class="alert alert-danger">
  10. <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
  11. </div>
  12. {% endif %}
  13.  
  14.  
  15. <div class="login-triangle"></div>
  16.  
  17. <h2 class="login-header">Log in</h2>
  18.  
  19. <form class="login-container">
  20. <p><input type="text" name="_username" placeholder="Phone"></p>
  21. <p><input type="password" name="_password" placeholder="Password"></p>
  22. <input type="hidden" name="_csrf_token"
  23. value="{{ csrf_token('authenticate') }}">
  24. <input type="checkbox" id="remember_me" name="_remember_me" checked/>
  25. <label for="remember_me">Keep me logged in</label>
  26. <p><input type="submit" value="Log in"></p>
  27. </form>
  28. </div>
  29.  
  30. </form>
  31.  
  32. {% endblock %}

Przykładowy kontroler wymagający autoryzacji
  1. <?php
  2.  
  3. namespace AppBundle\Controller;
  4.  
  5.  
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8.  
  9. class DefaultController extends Controller
  10. {
  11. /**
  12.   * @Route("/", name="homepage")
  13.   */
  14. public function indexAction()
  15. {
  16. if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
  17. throw $this->createAccessDeniedException();
  18. }
  19. return $this->render('base.html.twig');
  20. }
  21. }


Chcę zrobić, aby po zamknięciu przeglądarki i ponownym otwarciu user był nadal zalogowany. Walczę z tym już bardzo długo i bez efektów sad.gif
Go to the top of the page
+Quote Post
ohm
post 7.03.2018, 00:03:54
Post #2





Grupa: Zarejestrowani
Postów: 618
Pomógł: 143
Dołączył: 22.12.2010

Ostrzeżenie: (0%)
-----


A co w cookies siedzi po zalogowaniu?
Go to the top of the page
+Quote Post
PawelC
post 7.03.2018, 00:32:27
Post #3





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

Ostrzeżenie: (0%)
-----


Kod
[
{
    "domain": "127.0.0.1",
    "hostOnly": true,
    "httpOnly": true,
    "name": "PHPSESSID",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": false,
    "session": true,
    "storeId": "0",
    "value": "scp7ak4tu6a8p7tij20orghlm2",
    "id": 1
},
{
    "domain": "127.0.0.1",
    "expirationDate": 1520983532.061238,
    "hostOnly": true,
    "httpOnly": true,
    "name": "REMEMBERME",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": false,
    "session": false,
    "storeId": "0",
    "value": "QXBwQnVuZGxlXEVudGl0eVxVc2VyOmNtVjJaWEp6WlRJd01UZz06MTUyMDk4MzUzMjpjMDM1YmJm
NGFkMzNjZjI4YWM1M2M0MTUxNDU4YjYwYTY5NDI2NWEzNTRiZjkxMjc2NWUwZDljYzIzMmQyODhm",
    "id": 2
}
]
To zapisuje do ciasteczek i jak widać normalnie je tworzy.

Ten post edytował ExPlOiT 7.03.2018, 00:41:24
Go to the top of the page
+Quote Post
franki01
post 25.03.2018, 00:56:09
Post #4





Grupa: Zarejestrowani
Postów: 508
Pomógł: 75
Dołączył: 2.11.2005
Skąd: Bydgoszcz

Ostrzeżenie: (0%)
-----


Może problemem nie jest sam mechanizm logowania, ale wymagany poziom uwierzytelnienia ustawiony dla strony? http://symfony.com/doc/3.4/security/rememb...rtain-resources - chodzi konkretnie o ten akapit i uprawnienie "IS_AUTHENTICATED_FULLY", które powoduje, że ZAWSZE trzeba się zalogować i wtedy zapamiętywanie hasła nie działa. Na stronie logowania, po restarcie przeglądarki, na symfony toolbarze masz pokazanego swojego użytkownika czy "anon."?

Ten post edytował franki01 25.03.2018, 00:56:23
Go to the top of the page
+Quote Post
PawelC
post 2.04.2018, 21:00:06
Post #5





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

Ostrzeżenie: (0%)
-----


Problemem był fakt, żesymfony domyślnie ma usernam i password i remember me tego oczekiwał, a ja miałem logowanie po numerze telefonu i haśle. Wystarczyło w getUsernam podmienić $this->username na $this->phone i działa smile.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 29.03.2024 - 15:52