Spring Security基本身份验证
最后修改:2020年8月19日
1.概述
本教程介绍如何设置,配置和自定义春天的基本身份验证。我们将建立在简单之上春季MVC示例,并使用Spring Security提供的基本验证机制保护MVC应用程序的UI。
进一步阅读:
2. Spring安全配置
我们可以使用Java配置配置Spring Security:
@configuration @enableWebrity公共类CustomWebSecurityConfigurerAdapter扩展了WebSecurityConfigurerAdapter {@autowired private mybasicauthenticationEntyPoint身份信息学;@autowired public void configureglobal(authentication managerbuilder auth)返回异常{auth.inmemoryauthentication().withuser(“user1”)。密码(passwordencoder()。对(“user1pass”))。授权(“role_user”);@Override受保护的void配置(httpsecurity http)抛出异常{http.authorizeRequests().antmatchers(“/ securitynone”)。perpicall().AnyRequest()。Authenticated().and().httpbasic().authenticationentrypoint(身份验证);http.addfilterApter(新的CustomFilter(),BasicAuthenticationFilter.class);@bean public passwordencoder passwordencoder(){return new bcryptpasswordencoder();}}
在这里我们正在使用httpbasic()元素定义基本身份验证,在内部配置()延伸的类的方法WebSeecurityConfigurerAdapter。
也可以使用XML来实现同样的:
http> <身份验证 - 提供程序> <用户名=“user1”password =“{noop} user1pass”权限=“角色_user”/> 用户服务> 身份验证 - 提供程序> authentication-manager>
这是什么相关的
这web.xml.在启用Spring Security的Web应用程序中已经讨论过春季注销教程。
3.消耗安全应用程序
这卷曲命令是我们用于消耗安全应用程序的Go-to工具。
首先,让我们试着请求/homepage.html.不提供任何安全凭证:
curl -i http:// localhost:8080 / spring-security-rest-basic-auth / api / foos / 1
我们回来了预期的401未经授权和认证挑战:
http / 1.1 401未经授权的服务器:Apache-Coyote / 1.1 Set-cookie:JSessionID = E5A8D3C16B65A0A007CFAACAEEE6916B;路径= / spring-security-mvc-basic-auth /;httponly www-realyicsion:基本realm =“spring安全应用程序”内容类型:文本/ html; charset = Utf-8内容长度:1061日期:星期三,2013年5月29日15:14:08 GMT
浏览器将解释此挑战并提示我们使用简单对话框,但自我们使用卷曲,这不是这种情况。
现在,让我们请求相同的资源 - 主页 - 但是提供凭证访问它:
curl -i -user user1:user1past http:// localhost:8080 / spring-security-rest-basic-auth / api / foos / 1
现在,服务器的响应是200 OK.和A一起曲奇饼:
http / 1.1 200 ok服务器:apache-coyote / 1.1 set-cookie:jsessionid = 301225c7ae7c74b0892887389996785d;路径= / spring-security-mvc-basic-auth /;htthonly content-type:text / html; charset = ISO-8859-1内容语言:en-US Content-Length:90日期:星期三,2013年5月29日GMT
从浏览器中,应用程序可以正常消费 - 唯一的区别是登录页面不再是一个硬要求,因为所有浏览器都支持基本身份验证并使用对话框提示用户凭证。
进一步配置 - t他入学点
默认情况下,BasicAuthenticationEntrypoint.由Spring Security配置返回一个完整页面401未经授权回复客户端。此HTML表示错误在浏览器中呈现良好,但它不太适合其他场景,例如可以优选JSON表示的REST API。
该命名空间足够灵活,对于此新要求以及解决此问题 - 可以覆盖入口点:
新的入口点被定义为标准bean:
@component公共类MyBasicaUthenticationEntryPoint扩展了BasicAuthenticationEntryPoint {@Override公共void开始(httpservletRequest请求,httpservletresponse响应,httpservletresponse响应,servicleexception {response.addheader(“www-revalyicate”,“基本realm =”“+ getRealmname()+”“);response.setstatus(httpservletresponse.sc_unauthorized);printwriter writer = response.getWriter();Writer.PrintLn(“HTTP状态401 - ”+ authex.getMessage());@override public void后续oderpropertiesset()抛出异常{setrealmname(“baeldung”);金宝搏188体育super.AfterPropertiesset();}}
通过直接写入HTTP响应,我们现在可以完全控制响应机构的格式。
5. Maven依赖项
以前讨论了春天安全的Maven依赖关系用Maven文章春天安全- 我们都需要两者春天安全网和Spring-Security-Config在运行时提供。
六,结论
在此示例中,我们通过Spring Security和Basic身份验证保护MVC应用程序。我们讨论了XML配置,我们使用简单的Curl命令使用应用程序。最后控制了确切的错误消息格式 - 从标准HTML错误页面移动到自定义文本或JSON格式。
可以找到本教程的全面实现GitHub项目- 这是一个基于Maven的项目,因此应该易于导入和运行。
当项目本地运行时,可以访问示例HTML:
http:// localhost:8080 / spring-security-rest-basic-auth / api / foos / 1。