Spring Security HTTP基本认证示例

论坛 期权论坛 脚本     
已经匿名di用户   2022-3-21 23:58   2371   0

配置HTTP基本身份验证后,Web浏览器将显示一个用于用户身份验证的登录对话框。 本教程向您展示如何在Spring Security中配置HTTP基本认证。

<http>
 <intercept-url pattern="/welcome*" access="ROLE_USER" />
 <http-basic />
  </http>

最后一个基于Spring Security表单的登录示例将被重用,但是切换身份验证以支持HTTP基本。

1.Spring安全

要启用HTTP基本功能,只需将“ form-login ”更改为“ http-basic ”标记。

<beans:beans xmlns="http://www.springframework.org/schema/security"
 xmlns:beans="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/security
 http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

 <!-- HTTP basic authentication in Spring Security -->
 <http>
  <intercept-url pattern="/welcome*" access="ROLE_USER" />
  <http-basic />
 </http>

 <authentication-manager>
    <authentication-provider>
        <user-service>
     <user name="mkyong" password="123456" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
 </authentication-manager>

</beans:beans>

完成,仅此而已。

2.演示

访问安全的URL时,浏览器将自动显示一个登录对话框。

网址:http:// localhost:8080 / SpringMVC / welcome

http basic example

下载源代码

下载它-Spring-Security-HTTP-Basic-Authentication-Example.zip (9 KB)

参考文献

  1. Spring Security你好世界示例
  2. 基于Spring Security表单的登录示例

翻译自: https://mkyong.com/spring-security/spring-security-http-basic-authentication-example/

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP