Jboss7.1 加入realm auth认证 bootsfaces 美化的登录页面
jboss-as-7.1.1.Final\standalone\configuration:
1, standalone.xml中 <security-domains>标签里面添加:
<security-domain name="myRealm" cache-type="default">
<authentication>
<login-module code="Remoting" flag="required">
<module-option name="usersProperties" value="${jboss.server.config.dir}/application-users.properties"/>
<module-option name="rolesProperties" value="${jboss.server.config.dir}/application-roles.properties"/>
<module-option name="realm" value="ApplicationRealm"/>
<module-option name="password-stacking" value="useFirstPass"/>
</login-module>
</authentication>
</security-domain>
使用application-users.properties,application-roles.properties中定义的用户和角色。
2,在war中 WEB-INF 中加入文件 jboss-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<security-domain>java:/jaas/myRealm</security-domain>
</jboss-web>
3,在 web.xml中加入:
<security-constraint>
<display-name>Example Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>guest</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>myRealm</realm-name>
</login-config>
<security-role>
<description> A user </description>
<role-name>guest</role-name>
</security-role>
另外需注意,如果使用 primefaces,bootsfaces,由于他们使用css样式或者js,ttf等文件,如果他们位于需要认证的目录,但是login.xhtml又需要访问。这时需要在web.xml中排除掉 secure-constaint. 由于这个原因,登录页面的用户图标一直不显示,还得我调试了一天b:icon。
用bootfaces做的登录页面:

<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:b="http://bootsfaces.net/ui" xmlns:ui="http://java.sun.com/jsf/facelets" > <h:head> <title>Sign In Template for BootsFaces</title> <meta name="author" content="Riccardo Massera"></meta> <style type="text/css"> .form-signin { margin: 0 auto; max-width: 330px; padding: 15px; } </style> </h:head> <h:body style="padding-top: 60px; background-color: #add;"> <b:container> <h:form id="login" onsubmit="action='j_security_check';" styleClass="form-signin"> <h2 class="form-signin-heading">请登陆</h2> <b:inputText id="j_username1" placeholder="Email address" fieldId="j_username" name="j_username"> <f:facet name="prepend"> <b:icon name="user" /> </f:facet> </b:inputText> <b:inputText id="j_password1" placeholder="Password" type="password" fieldId="j_password" name="j_password"> <f:facet name="prepend"> <b:iconAwesome name="key" /> </f:facet> </b:inputText> <b:commandButton look="primary btn-block" id="submit" value="登录" ajax="false" size="lg"/> </h:form> </b:container> </h:body> </html>
primefaces使用这个登录页面:

<h:form id="login" onsubmit="action='j_security_check';" prependId="false"> <h:panelGrid columns="2"> <p:outputLabel for="j_username" value="Username" /> <p:inputText id="j_username" /> <p:outputLabel for="j_password" value="Password" /> <p:password id="j_password" /> <p:commandButton id="submit" value="Login" ajax="false"/> </h:panelGrid> </h:form>
web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/faces/index.xhtml</welcome-file> </welcome-file-list> <security-constraint> <display-name>Security Constraint</display-name> <web-resource-collection> <web-resource-name>Protected Area</web-resource-name> <url-pattern>/faces/*</url-pattern> <http-method>DELETE</http-method> <http-method>GET</http-method> <http-method>POST</http-method> <http-method>PUT</http-method> </web-resource-collection> <auth-constraint> <role-name>Manager</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>FORM</auth-method> <realm-name>dbdomain</realm-name> <form-login-config> <form-login-page>/faces/views/login.xhtml</form-login-page> <form-error-page>/faces/views/loginError.xhtml</form-error-page> </form-login-config> </login-config> <security-role> <role-name>Manager</role-name> </security-role> <!-- not secure page --> <security-constraint> <display-name>UnSecuredPages</display-name> <web-resource-collection> <!-- *.js no use. --> <web-resource-name>login-required</web-resource-name> <url-pattern>*.js</url-pattern> </web-resource-collection> <web-resource-collection> <web-resource-name>ttf</web-resource-name> <url-pattern>/faces/fonts/*</url-pattern> </web-resource-collection> <web-resource-collection> <web-resource-name>login-required1</web-resource-name> <url-pattern>/faces/javax.faces.resource/*</url-pattern> </web-resource-collection> </security-constraint> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Production</param-value> </context-param> <session-config> <session-timeout> 0 </session-timeout> </session-config> <context-param> <param-name>net.bootsfaces.get_fontawesome_from_cdn</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>primefaces.THEME</param-name> <param-value>bootstrap</param-value> </context-param> <context-param> <param-name>primefaces.UPLOADER</param-name> <param-value>auto</param-value> </context-param> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> </web-app>
参考:
http://blog.sina.com.cn/s/blog_7253d65401018syh.html
http://www.cnblogs.com/davidwang456/p/3897684.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?