简体   繁体   English

如果用户未登录,则将他们重定向到登录页面

[英]Redirect users to login page if they are not login

When user is not logged in and he enter right url for login page then he goes on login page otherwise when user enter other url he will be redirected to error page instead of redirecting to login page. 当用户未登录时,他在登录页面上输入正确的URL,然后进入登录页面;否则,当用户输入其他URL时,他将被重定向到错误页面,而不是重定向到登录页面。 how to fix this? 如何解决这个问题?

You must use @Secure annotation in your controller. 您必须在控制器中使用@Secure批注。 This will redirect user to login page if not authorized. 如果未经授权,这会将用户重定向到登录页面。 This Code : 该代码:

@Secured(AuthenticatedVoter.IS_AUTHENTICATED_FULLY)
class CustomerController{
    ....
}

will redirect everyone how try to access customer view page unless they are logged in. they will be redirected to login page after trying to access this page. 除非他们已登录,否则将重定向所有人如何尝试访问客户视图页面。尝试访问此页面后,他们将被重定向到登录页面。 Although it is just a simple authentication and usually you need to do more than that, but it will do what you want. 尽管这只是一个简单的身份验证,通常您需要做的还不止这些,但是它将满足您的要求。

For your first question, the above answer is fine, but if you want to restrict more specfically, you can use this too 对于第一个问题,上面的答案很好,但是如果您想更具体地限制,也可以使用它

First you should install Grails Spring Security plugin 首先,您应该安装Grails Spring Security插件

class UserController {

  // allow to access everyone
  def index() {
  }

  @Secured([UserRole.ROLE_USER,UserRole.ROLE_ADMIN,...])
  def account() {
  }
}

Note , if you are giving @Secured at the top of the class, will restrict the whole class from access 请注意 ,如果您在@Secured顶部提供@Secured ,则会限制整个课程的访问权限

For your second question, you use UrlMapping , for example 对于第二个问题,例如,使用UrlMapping

"404"(controller:"yourController", action:"yourErrorPageAction")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM