简体   繁体   English

Java Servlet 如何按用户角色过滤页面

[英]Java Servlet How to filter pages by User Role

I'm coding a security web app.我正在编写一个安全 web 应用程序。 And I have a problem.我有一个问题。 For example, there is a pages called " Manager (JSP) ".例如,有一个页面叫做“Manager (JSP)”。 I only want admins to see this page.我只希望管理员看到这个页面。

When user login the website, session create.当用户登录网站时,session 创建。

if(dao.check(uname, pass)) {
        UserAccount user = new UserAccount();
        user.setUsername(uname);
        HttpSession session = request.getSession();
        session.setAttribute("username", uname);
        response.sendRedirect("welcome.jsp");
    }

Heres the UserAccount Class:这是用户帐户 Class:

public class UserAccount {
private String username;
private String password;
private List<String> roles;

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public List<String> getRoles() {
    return roles;
}
public void setRoles(List<String> roles) {
    this.roles = roles;
}

} }

And i have a Filter class which implements Filter.我有一个过滤器 class 实现过滤器。 How program get understand who is admin or not and let to see manager page?程序如何了解谁是管理员并让其查看管理员页面?

You can use a boolean attribute in your UserAccount class, this attribute will hold a value of true if the use is an admin, and false if he is not您可以在 UserAccount class 中使用 boolean 属性,如果用户是管理员,则此属性的值为true ,如果不是管理员,则为 false

public class UserAccount {
private String username;
private String password;
private List<String> roles;
private boolean is_admin;
...}

The redirection to manager page will see first if the current user is admin or not, so he will be denied if he's not an admin重定向到经理页面将首先查看当前用户是否为管理员,因此如果他不是管理员,他将被拒绝


NB: i see that you are using a List of roles, so maybe you're using a Role-Based access control ( like RBAC or RABAC )注意:我看到您正在使用角色列表,所以也许您正在使用基于角色的访问控制(如RBACRABAC

In that case maybe you're giving admin permissions through his roles, so here the Filter method will see the user's roles list and check if it contains the role of admin, and if yes it will redirect into the manager page在这种情况下,您可能通过他的角色授予管理员权限,所以这里的 Filter 方法将查看用户的角色列表并检查它是否包含管理员角色,如果是,它将重定向到管理器页面

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

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