gpt4 book ai didi

jsf - JSF 中的基本安全性

转载 作者:行者123 更新时间:2023-12-03 07:49:30 29 4
gpt4 key购买 nike

我想看一个简单的登录应用程序,不像this那么简单尽管。

我想要实现的是对 JSF 的工作原理的理解,我开发了很多 ASP.NET,您可以在其中隐藏代码,并且您可以在其中检查是否在登录时创建了 session 。

JSF 中的类似解决方案会很棒。

这基本上是我想要实现的目标:

  • 登录页面
  • 如果正常
  • 创建 session 并返回“成功”
  • 如果失败
  • 返回“失败”

  • (“成功”和失败被映射到faces-config.xml)

    在成功页面我想成为 某些 用户已登录,因此如果您没有获得正确的 session ,则应该无法导航到“success.jspx”。

    最佳答案

    除了能够使用组件 rendered 之类的东西之外,核心 JSF 中没有固有的身份验证功能。面向基于角色的安全性的属性。

    默认情况下,JSF 应用程序依赖于与包含它的 Web 组件相同的容器管理的安全机制 ( JEE5 tutorial )。 3rd 方框架,如 Seam可以提供替代方案。

    如果要添加自己的应用程序安全性,servlet filter是较简单的机制之一。

    此过滤器保护 restricted 下的资源web.xml 中定义的目录:

      <filter>
    <filter-name>AuthenticationFilter</filter-name>
    <filter-class>restricted.AuthenticationFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>AuthenticationFilter</filter-name>
    <url-pattern>/restricted/*</url-pattern>
    </filter-mapping>

    过滤器类实现:
    public class AuthenticationFilter implements Filter {
    private FilterConfig config;

    public void doFilter(ServletRequest req, ServletResponse resp,
    FilterChain chain) throws IOException, ServletException {
    if (((HttpServletRequest) req).getSession().getAttribute(
    AuthenticationBean.AUTH_KEY) == null) {
    ((HttpServletResponse) resp).sendRedirect("../restricted_login.faces");
    } else {
    chain.doFilter(req, resp);
    }
    }

    public void init(FilterConfig config) throws ServletException {
    this.config = config;
    }

    public void destroy() {
    config = null;
    }
    }

    faces-config.xml 中定义的登录 bean :
    public class AuthenticationBean {
    public static final String AUTH_KEY = "app.user.name";

    private String name;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public boolean isLoggedIn() {
    return FacesContext.getCurrentInstance().getExternalContext()
    .getSessionMap().get(AUTH_KEY) != null;
    }

    public String login() {
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(
    AUTH_KEY, name);
    return "secret";
    }

    public String logout() {
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
    .remove(AUTH_KEY);
    return null;
    }
    }
    restricted_login.jsp 中的 JSF 登录表单页:
      <f:view>
    <p><a href="restricted/secret.faces">try to go to secret
    page</a></p>
    <h:form>
    Username:
    <h:panelGroup rendered="#{not authenticationBean.loggedIn}">
    <h:inputText value="#{authenticationBean.name}" />
    <h:commandButton value="login"
    action="#{authenticationBean.login}" />
    </h:panelGroup>
    <h:commandButton value="logout"
    action="#{authenticationBean.logout}"
    rendered="#{authenticationBean.loggedIn}" />
    </h:form>
    </f:view>

    (选择重定向 URL/机制是为了简洁而不是任何类型的最佳实践;更多选项见 Servlet API。)

    关于jsf - JSF 中的基本安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1470591/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com