如何使用javaconfig(无XML)专门为摘要式身份验证配置Spring 4.0和Spring Security(3.2.0)?我正在使用下面的配置类,但所有请求都被HTTP 401拒绝,"Nonce应该产生两个令牌,但是(...消息就在那里停止)".
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfigurationDigest extends WebSecurityConfigurerAdapter
{
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
{
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests().antMatchers("/**").authenticated().and().addFilter(digestAuthenticationFilter(digestEntryPoint()));
}
@Override
@Bean
public UserDetailsService userDetailsServiceBean() throws Exception
{
return super.userDetailsServiceBean();
}
public DigestAuthenticationFilter digestAuthenticationFilter(DigestAuthenticationEntryPoint digestAuthenticationEntryPoint) throws Exception
{
DigestAuthenticationFilter digestAuthenticationFilter = new DigestAuthenticationFilter();
digestAuthenticationFilter.setAuthenticationEntryPoint(digestEntryPoint());
digestAuthenticationFilter.setUserDetailsService(userDetailsServiceBean());
return digestAuthenticationFilter;
}
@Bean
public DigestAuthenticationEntryPoint digestEntryPoint()
{
DigestAuthenticationEntryPoint digestAuthenticationEntryPoint = new DigestAuthenticationEntryPoint();
digestAuthenticationEntryPoint.setKey("mykey");
digestAuthenticationEntryPoint.setRealmName("myrealm");
return digestAuthenticationEntryPoint;
}
}
Run Code Online (Sandbox Code Playgroud)
我试图通过包含标题在客户端授权: …
java spring spring-security digest-authentication spring-java-config
我最近在我为ASP.NET公司创建的Intranet网站/应用程序上启用了摘要式身份验证.
我这样做的原因是因为Windows身份验证似乎只适用于某些用户,而不适用于其他用户.我无法弄清楚为什么我也不了解IIS以试图追踪问题.经过一些反复试验,我发现摘要式身份验证似乎给了我想要的行为.即:仅允许域中具有有效帐户的用户使用其凭据登录网站.
现在的问题是,Firefox(3+)似乎要求用户对发送到服务器的每个HTTP请求进行身份验证.这似乎不会出现在Internet Explorer(6+)或Chrome中.
我试过寻找解决方案,但我总是到达死胡同.我会找到关于这个问题的讨论,每个发布的解决方案都会导致死链接......或者它在Expert Exchange上,我无权查看解决方案.
这个问题似乎与我所阅读的内容有关(不同之处)是不同浏览器发送身份验证标头的方式与IIS如何解释它们的关系.我不知道我能做些什么来改变这个呢?我发现的其中一个解决方案提到编写一个ISAPI过滤器来解决这个问题,但当然完成过滤器的链接已被破坏,我不知道如何自己制作一个.
我已经尝试在about:config中弄乱NTLM和其他与auth相关的字符串,试图强迫Firefox信任我的服务器,但这似乎也不起作用.
从我读过的其他一些资料来看,如果我切换回Windows身份验证,似乎一切都应该有效,但后来我回到了第一方,验证只适用于某些用户,而不适用于其他用户.
这两个问题的解决方案对我都有用,但我对Windows身份验证问题的信息很少.如果有人可以指导我追踪问题,我也很乐意为此发布更多信息.
以下是我发现的讨论看似同样问题的网址.(对不起,我无法将它们全部链接起来,否则不会让我发帖)
我正在使用java(和scala)开发和测试一个简单的直接客户端 - 服务器应用程序.
该服务器是基于com.sun.net.httpserver.HttpServer并允许文件上传通过使用POST和PUT操作的基本RESTful接口.使用我们自己实现的摘要式身份验证限制上传操作,经过测试并在浏览器中工作,卷曲和Apache HttpClient.
在上传客户端包裹Apache HttpClient 4.1.2并执行放完了HTTP操作来上传文件的实体.文件的内容类型application/xml在标题中指定,一次只上载一个文件.
上传不同大小的文件时,可能会出现奇怪的行为:
java.net.SocketException: Broken pipe.(确切的临界尺寸未知,因为我手动创建了不同尺寸的文件以接近最大工作尺寸)
管道损坏的原因是,客户端以某种方式忽略了www-authenticate响应的上传文件,如服务器日志所记录的那样."忽略"意味着它只发送多个(4)消息,根本不包含认证头.但是较小的文件运行良好,并且客户端在响应之后立即正确地发送具有适当质询 - 响应的身份验证请求www-authenticate.
上传工作卷曲与各种大小的文件,所以没有问题.
所以在这一点上,人们可以说:"你的客户端存在一些错误." 好吧,我有点希望如此,但我也尝试过一个开源的java RESTclient(也包装apache httpclient),它有完全相同的行为!
我们通过互联网使用这个客户端尝试了它,它也和描述的相同.所以现在,我只是希望我错过了设置一些重要的Apache HttpClient导致这种错误行为的东西,而开源RESTclient的开发者也错过了它......任何想法都可能会很棒!
我想通过.htaccess来保护文件夹.以下不起作用.它在浏览器中显示登录对话框,但看起来好像用户名和密码不匹配.当我将方法更改为Basic时,它工作正常.
AuthUserFile /home/pass/.htpasswd
AuthName "Login"
AuthType Digest
Require valid-user
Run Code Online (Sandbox Code Playgroud) .htaccess password-protection digest-authentication .htpasswd
对于testapp在web.xml中具有以下内容的webapp (除其他外)
<security-constraint>
<web-resource-collection>
<web-resource-name>My JSP</web-resource-name>
<url-pattern>*.secured</url-pattern>
<url-pattern>/login</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>mobileusers</role-name>
</auth-constraint>
<!--
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
-->
</security-constraint>
<login-config>
<auth-method>DIGEST</auth-method>
<realm-name>Identity</realm-name>
</login-config>
<security-role>
<description>
No Description
</description>
<role-name>mobileusers</role-name>
</security-role>
Run Code Online (Sandbox Code Playgroud)
考虑以下两个Tomcat领域配置:
配置1 - JDBC领域:
在 .../webapps/testapp/META-INF/context.xml
<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="com.mysql.jdbc.Driver"
connectionName="mysqluser"
connectionPassword="redacted"
connectionURL="jdbc:mysql://192.168.1.5/testdb?autoReconnectForPools=true&characterEncoding=UTF-8"
digest="MD5"
userTable="Users"
userNameCol="name"
userCredCol="password"
userRoleTable="Users"
roleNameCol="roleName"
/>
Run Code Online (Sandbox Code Playgroud)
配置2 - DataSource领域:
在.../webapps/testapp/META-INF/context.xml:
<Realm className="org.apache.catalina.realm.DataSourceRealm"
digest="MD5"
userTable="Users"
userNameCol="name"
userCredCol="password"
userRoleTable="Users"
roleNameCol="roleName"
dataSourceName="jdbc/testDB"
/>
Run Code Online (Sandbox Code Playgroud)
并在.../conf/context.xml:
<Resource
name="jdbc/testDB"
auth="Container"
type="javax.sql.DataSource"
removeAbandoned="true"
removeAbandonedTimeout="15"
maxActive="5"
maxIdle="5"
maxWait="7000" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用HTTP客户端实现摘要身份验证,但是目前无法正常工作。
有人可以检查此代码是否正确吗?出于测试目的,我使用http://httpbin.org/,但我得到的只是HTTP/1.1 401 Unauthorized。
这是示例代码:
private static void doDigestAuth() throws ClientProtocolException,
IOException,
AuthenticationException,
MalformedChallengeException
{
HttpHost target = new HttpHost("httpbin.org", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(
"user", "passwd"));
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
try {
HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd");
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local
// auth cache
DigestScheme digestAuth = new DigestScheme();
// …Run Code Online (Sandbox Code Playgroud) 我有一个正在进行的项目,通过 opencv VideoCapture 访问多个 IP 摄像机,适用于大多数摄像机。
我有一个新的大华 PTZ 摄像机,它使用摘要身份验证,OpenCV 中的 VideoCapture 无法打开它。通过 WireShark,我可以看到相机返回 401 Unaothorized。
我在 OpenCV 文档中没有找到任何有关身份验证问题的内容。
也许我需要使用 OpenCV 以外的其他东西来解决这个问题?
这是最低工作代码(如果您有相机要测试)。
#include <iostream>
#include <imgproc.hpp>
#include <opencv.hpp>
#include <highgui.hpp>
using namespace std;
using namespace cv;
int main(){
while(1){
VideoCapture cap("http://login:password@111.111.111.111/cgi-bin/snapshot.cgi");
if(!cap.isOpened()){
cout << "bug" << endl;
continue;
}
Mat frame;
cap >> frame;
imshow("test", frame);
}
}
Run Code Online (Sandbox Code Playgroud)
这是相机的响应:
我在stackoverflow上发现了许多关于摘要式身份验证的问题.我找不到摘要验证如何防止重放攻击?我使用fiddler工具拦截对服务器的http请求.我使用相同的工具将请求重播到服务器,但服务器要求进行身份验证.
我需要准确理解如何实现防止重放攻击.服务器如何检测http请求的任何重播?
任何链接/资源将不胜感激.
到目前为止我有这个代码:
private class DownloadWebPageTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... theParams)
{
String myUrl = theParams[0];
String myEmail = theParams[1];
String myPassword = theParams[2];
HttpPost post = new HttpPost(myUrl);
post.addHeader("Authorization","Basic "+ Base64.encodeToString((myEmail+":"+myPassword).getBytes(), 0 ));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = null;
try
{
response = client.execute(post, responseHandler);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null)
{
response += s;
}
}
catch …Run Code Online (Sandbox Code Playgroud) 我有一个实现REST api的flask应用程序。由于某些原因,我正在使用HTTP摘要式身份验证。我已经使用了Flask-HTTPAuth库来实现摘要身份验证,并且它可以工作。但是,我无法在单元测试中进行身份验证。
对于单元测试,在设置身份验证之前,我正在执行以下操作:
class FooTestCase(unittest.TestCase):
def setUp(self):
self.app = foo.app.test_client()
def test_root(self):
response = self.app.get('/')
# self.assert.... blah blah blah
Run Code Online (Sandbox Code Playgroud)
在实施身份验证之前,这很好。现在,我得到一个401,它应作为摘要auth请求的初始响应。我进行了搜索,并遵循了一些与http基本身份验证相关的建议(使用参数data = {#various stuff}和follow_redirects = True),但是我没有成功。
在这种情况下,有谁知道如何实施单元测试?
python digest-authentication flask python-unittest flask-httpauth