कोर जेएसएफ में घटक rendered
जैसी चीजों का उपयोग करने में सक्षम होने से परे भूमिका-आधारित सुरक्षा की दिशा में कोई अंतर्निहित प्रमाणीकरण कार्यक्षमता नहीं है।
डिफ़ॉल्ट रूप से, एक जेएसएफ एप्लिकेशन उसी कंटेनर-प्रबंधित सुरक्षा तंत्र पर निर्भर करता है जिसमें वेब घटक होता है जिसमें यह शामिल होता है (JEE5 tutorial)। Seam जैसे तृतीय पक्ष ढांचे विकल्प प्रदान कर सकते हैं।
यदि आप अपनी खुद की एप्लिकेशन सुरक्षा जोड़ना चाहते हैं, तो servlet filter सरल तंत्र में से एक है।
<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
में परिभाषित:
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
निर्देशिका के रूप में web.xml
में परिभाषित के तहत संसाधनों की रक्षा करता है वें में जेएसएफ लॉगिन फॉर्म ई restricted_login.jsp
पेज:
<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 देखें।)
संबंधित: http://stackoverflow.com/questions/2206911/best-way-for-user- प्रमाणीकरण-on-javaee-6-using-jsf-2-0/2207147#2207147 या यदि आप खुले हैं शिरो जैसे तीसरे पक्ष के पुस्तकालयों के लिए, फिर http://balusc.blogspot.com/2013/01/apache-shiro-is-it-ready-for-java-ee-6.html – BalusC