JAVA基础(28) web.xml中<security-constraint>和四种认证类型

本文详细介绍了web.xml文件中&lt;security-constraint&gt;元素及其子元素的作用与配置方法,包括如何设置资源访问权限、认证方式及数据传输保护措施。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<security-constraint> 的子元素 <http-method> 是可选的,

如果没有 <http-method> 元素,这表示将禁止所有 HTTP 方法访问相应的资源。 

子元素 <auth-constraint> 需要和 <login-config> 相配合使用,但可以被单独使用。

如果没有 <auth-constraint> 子元素,这表明任何身份的用户都可以访问相应的资源。

也就是说,如果 <security-constraint> 中没有 <auth-constraint> 子元素的话,配置实际上是不起中用的。

如果加入了 <auth-constraint> 子元素,但是其内容为空,这表示所有身份的用户都被禁止访问相应的资源。 

web.xml:

<security-constraint>   
  <display-name>   
  baseporject</display-name>   
  <web-resource-collection>   
   <web-resource-name>baseproject</web-resource-name>   
   <url-pattern>*.jsp</url-pattern>   
   <url-pattern>*.do</url-pattern>   
   <http-method>GET</http-method>   
   <http-method>PUT</http-method>   
   <http-method>HEAD</http-method>   
   <http-method>TRACE</http-method>   
   <http-method>POST</http-method>   
   <http-method>DELETE</http-method>   
   <http-method>OPTIONS</http-method>   
  </web-resource-collection>   
  <auth-constraint>   
   <description>   
   baseproject</description>   
   <role-name>All Role</role-name>   
  </auth-constraint>   
  <user-data-constraint>   
   <transport-guarantee>NONE</transport-guarantee>   
  </user-data-constraint>   
</security-constraint>   
<login-config>Xml代码   

<!--四种验证方式,附在最后有说明-->    
  <auth-method>FORM</auth-method>    
  <form-login-config>    
   <form-login-page>/login.html</form-login-page>    
   <form-error-page>/error.html</form-error-page>    
  </form-login-config>    
</login-config>    
<security-role>    
  <role-name>All Role</role-name>    
</security-role>
 security-constriaint元素的用途是用来指示服务器使用何种验证方法了.此元素在web.xml中应该出现在login-config 的紧前面。它包含是个可能的子元素,分别是:web-resource-collection、auth-constraint、user-data- constraint和display-name。下面各小节对它们进行介绍。 
1. web-resource-collection 
  此元素确定应该保护的资源,所有security-constraint元素都必须包含至少一个web-        resource-collection项.此元素由一个给出任意标识名称的web-resource-name元素、一个确定应该保护URL    的url-pattern元素、一个指出此保护所适用的HTTP命令(GET、POST等,缺省为所有方法)的http-method元素和一个提供资料 的可选description元素组成。 
    重要的是应该注意到,url-pattern仅适用于直接访问这些资源的客户机。特别是,它不适合于通过MVC体系结构利用RequestDispatcher来访问的页面,或者不适合于利用类似jsp:forward的手段来访问的页面。 
2. auth-constraint 
元素却指出哪些用户应该具有受保护资源的访问权。此元素应该包含一个或多个标识具有访问权限的用户类别role-name元素,以及包含(可选)一个描述角色的description元素。 
3. user-data-constraint 
这个可选的元素指出在访问相关资源时使用任何传输层保护。它必须包含一个transport-guarantee子元素(合法值为NONE、 INTEGRAL或CONFIDENTIAL),并且可选地包含一个description元素。transport-guarantee为NONE值将 对所用的通讯协议不加限制。INTEGRAL值表示数据必须以一种防止截取它的人阅读它的方式传送。虽然原理上(并且在未来的HTTP版本中),在 INTEGRAL和CONFIDENTIAL之间可能会有差别,但在当前实践中,他们都只是简单地要求用SSL。 
4 四种认证类型:

BASIC:HTTP规范,Base64   
<web-app>   
    ......   
    <login-config>   
        <auth-method>BASIC</auth-method>   
    </login-config>   
    ......   
</web-app>   
  
DIGEST:HTTP规范,数据完整性强一些,但不是SSL   
<web-app>   
    ......   
    <login-config>   
        <auth-method>DIGEST</auth-method>   
    </login-config>   
    ......   
</web-app>   
  
CLIENT-CERT:J2EE规范,数据完整性很强,公共钥匙(PKC)   
<web-app>   
    ......   
    <login-config>   
        <auth-method>CLIENT-CERT</auth-method>   
    </login-config>   
    ......   
</web-app>   
FORM:J2EE规范,数据完整性非常弱,没有加密,允许有定制的登陆界面。 
<web-app> 
    ...... 
    <login-config> 
        <auth-method>FORM</auth-method> 
        <form-login-config> 
            <form-login-page>/login.html</form-login-page> 
            <form-error-page>/error.jsp</form-error-page> 
        </form-login-config> 
    </login-config> 
    ...... 
</web-app> 
这里的 FORM 方式需要说明的是 登录页面的固定的元素:login.html 

<form name="loginform" method="post" action="j_security_check">   
  
<INPUT name="j_username" type="text">   
  
<INPUT name="j_password" TYPE="password">   
  
<input type="submit" value="登 录" >   
  
</form> 
form 的action 必须是j_security_check, method="post", 用户名 name="j_username" , 密码name="j_password"  这些都是固定的元素 。





<?xml version="1.0" encoding="UTF-8"?> <!--<web-app xmlns="http://java.sun.com/xml/ns/j2ee"--> <!-- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"--> <!-- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"--> <!-- version="2.4">--> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5.xsd" version="5.0"> <display-name>howie2</display-name> <filter> <filter-name>hmvc</filter-name> <filter-class>com.howie.hmvc.DispatcherFilter</filter-class> </filter> <filter-mapping> <filter-name>hmvc</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <filter-mapping> <filter-name>hmvc</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping> <filter-mapping> <filter-name>hmvc</filter-name> <url-pattern>*.jhtml</url-pattern> </filter-mapping> <filter-mapping> <filter-name>hmvc</filter-name> <url-pattern>*.aj</url-pattern> </filter-mapping> <filter-mapping> <filter-name>hmvc</filter-name> <url-pattern>*.ar</url-pattern> </filter-mapping> <jsp-config> <taglib> <taglib-uri>asy</taglib-uri> <taglib-location>/WEB-INF/anshuyun.tld</taglib-location> </taglib> </jsp-config> <error-page> <error-code>404</error-code> <location>/error/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/error/500.jsp</location> </error-page> <session-config> <session-timeout>20</session-timeout> <!-- <cookie-config> <secure>true</secure> </cookie-config> --> </session-config> <security-constraint> <web-resource-collection> <url-pattern>/*</url-pattern> <http-method>PUT</http-method> <http-method>DELETE</http-method> <http-method>HEAD</http-method> <http-method>OPTIONS</http-method> <http-method>TRACE</http-method> </web-resource-collection> <au
03-20
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>员工信息</title> <script type="text/javascript" th:src="@{/js/vue.global.js}"></script> </head> <body> <table id="dataTable" border="1" cellspacing="0" cellpadding="0" style="text-align:center"> <tr> <th colspan="5">员工信息</th> </tr> <tr> <th>编号</th> <th>姓名</th> <th>邮箱</th> <th>性别</th> <th>操作</th> </tr> <tr th:each="employee : ${employeeList}"> <td th:text="${employee.id}"></td> <td th:text="${employee.lastName}"></td> <td th:text="${employee.email}"></td> <td th:text="${employee.gender}"></td> <td> <a @click="deleteEmployee" th:href="@{'/employee/' + ${employee.id}}">删除</a> <a href="">修改</a> </td> </tr> </table> <form id="deleteForm" method="post"> <input type="hidden" name="_method" value="delete"> </form> <script type="text/javascript"> var vue = new Vue({ el: "#dataTable", methods: { deleteEmployee: function (event) { var deleteForm = document.getElementById("deleteForm"); deleteForm.action = event.target.href; deleteForm.submit(); event.preventDefault(); } } }); </script> </body> </html> //删除员工信息 @RequestMapping(value = "/employee/{id}",method = RequestMethod.DELETE) public String deleteEmployee(@PathVariable("id") Integer id){ employeeDao.delete(id); return "redirect:/employee"; } <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置编码过滤器--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置 处理请求方式put,delete的HiddenHttpmethodFilter--> <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置SpringMVC的前端控制器DispatcherServlet--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--开启组件扫描--> <context:component-scan base-package="com.atguigu.rest"/> <!--配置thymeleaf视图解析器--> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8"/> </bean> </property> </bean> </property> </bean> <!--配置视图控制器--> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> <!--开放对静态资源的访问呢--> <mvc:default-servlet-handler></mvc:default-servlet-handler> <!--开启mvc注解驱动--> <mvc:annotation-driven></mvc:annotation-driven> </beans> 报错 405 找不到方法
最新发布
07-05
]. The system is vulnerable to security attacks, since it trusts certificates signed by the demo trusted CA.> <Mar 17, 2025 5:13:50,408 PM CST> <Warning> <HTTP> <BEA-101403> <For security constraint with url-pattern /* in web application rtgsMOP.war, only the http methods "TRACE HEAD PURGE LOCK PROPFIND UNLINK COPY OPTIONS VIEW PUT PATCH DELETE LINK UNLOCK " are covered.> <Mar 17, 2025 5:13:51,500 PM CST> <Warning> <HTTP> <BEA-101403> <For security constraint with url-pattern /* in web application bankMOP.war, only the http methods "TRACE HEAD PURGE LOCK PROPFIND UNLINK COPY OPTIONS VIEW PUT PATCH DELETE LINK UNLOCK " are covered.> <Mar 17, 2025 5:13:52,098 PM CST> <Critical> <HTTP> <BEA-101024> <[ServletContext@1374383048[app:bankMOP module:bankMOP.war path:null spec-version:4.0]] Unsupported error status code for the error-page element in web.xml. java.lang.ArrayIndexOutOfBoundsException: -99 at weblogic.servlet.internal.ErrorManager.registerError(ErrorManager.java:51) at weblogic.servlet.internal.ErrorManager.registerErrorPages(ErrorManager.java:87) at weblogic.servlet.internal.WebAppServletContext.activateFromDescriptors(WebAppServletContext.java:1199) at weblogic.servlet.internal.WebAppServletContext.activate(WebAppServletContext.java:3241) at weblogic.servlet.internal.WebAppModule.activateContexts(WebAppModule.java:1879) Truncated. > <Mar 17, 2025 5:13:52,099 PM CST> <Critical> <HTTP> <BEA-101024> <[ServletContext@1150367507[app:rtgsMOP module:rtgsMOP.war path:null spec-version:4.0]] Unsupported error status code for the error-page element in web.xml. java.lang.ArrayIndexOutOfBoundsException: -99 at weblogic.servlet.internal.ErrorManager.registerError(ErrorManager.java:51) at weblogic.servlet.internal.ErrorManager.registerErrorPages(ErrorManager.java:87) at weblogic.servlet.internal.WebAppServletContext.activateFromDescriptors(WebAppServletContext.java:1199) at weblogic.servlet.internal.WebAppServletContext.activate(WebAppServletContext.java:3241) at weblogic.servlet.internal.WebAppModule.activateContexts(WebAppModule.java:1879) Truncated. > <Mar 17, 2025 5:13:53,098 PM CST> <Warning> <HTTP> <BEA-101403> <For security constraint with url-pattern /* in web application rtgsMOP.war, only the http methods "TRACE HEAD PURGE LOCK PROPFIND UNLINK COPY OPTIONS VIEW PUT PATCH DELETE LINK UNLOCK " are covered.> <Mar 17, 2025 5:13:53,157 PM CST> <Warning> <HTTP> <BEA-101403> <For security constraint with url-pattern /* in web application bankMOP.war, only the http methods "TRACE HEAD PURGE LOCK PROPFIND UNLINK COPY OPTIONS VIEW PUT PATCH DELETE LINK UNLOCK " are covered.> 帮我分析一下这个weblogic server 启动日志
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值