我目前正在为学校项目,Spring Boot后端和AngularJS前端创建一个简单的应用程序,但是我似乎无法解决安全问题.
登录工作完美,但是当我输入错误的密码时,默认的登录弹出窗口显示出来,这有点烦人.我已经尝试了注释'BasicWebSecurity'并将httpBassic置于禁用状态,但没有结果(意味着登录过程根本不起作用).
我的安全类:
package be.italent.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.csrf.CsrfFilter;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.WebUtils;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override …Run Code Online (Sandbox Code Playgroud) java spring-security basic-authentication angularjs spring-boot
我有一个只能通过http basic auth访问的网页.如何在该页面的javascript中找出基本的auth用户名.即当有人访问它时(登录后),我想制作一个弹出窗口,上面写着"你好,你当前是以用户$ USERNAME身份登录的"
我需要从XML-RPC Web服务中获取数据.
new XmlSlurper().parse("http://host/service") 工作正常,但现在我有一个特殊的服务,需要基本的HTTP身份验证.
如何为parse()方法设置用户名和密码,或修改请求的HTTP标头?
使用http://username:password@host/service没有帮助 - 我仍然得到java.io.IOException: Server returned HTTP response code: 401 for URL例外.
谢谢
我已经使用Spring MVC构建了一个REST API后端,并使用Spring Security安装了基本的Auth.
我想从Javascript客户端对REST API进行跨域ajax调用.我不想使用JSONP,因为我不想限于GET调用.我使用CORS,我在服务器端放置了正确的标题.
假设我的REST API在域localhost:8087上,我的客户端在localhost:8086上,这是跨域调用.
在我的Javascript客户端中,我使用jQuery进行ajax调用:
<script>
$.ajax ({
url: "http://localhost:8087/SpringMVC/users/user1",
beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", "Basic xxxxxxxxxxxx"); },
success: function(val) { console.log(val); alert("success" + val); },
error: function(val) { console.log(val); alert("error" + val); }
});
</script>
Run Code Online (Sandbox Code Playgroud)
我的问题是jQuery没有在HTTP请求中发送Authorization标头,我不知道为什么.我不明白因为我在beforeSend方法中这样做,所以它应该在HTTP请求中.结果:我有401错误.
当我尝试从同一域localhost:8087,这不再是跨域的脚本,我没有问题.
这怎么可能 ?
我的脚本只是一个测试.我不打算将我的用户名/密码放在客户端.但我想测试如何对基本的auth保护的REST API进行ajax调用.我想我必须在服务器端发送安全我的用户名/密码,REST API发送给我一个cookie,我不需要再传递用户名/密码,我的下一个ajax调用REST API.我对吗 ?
我已经使用Chrome Advanced REST客户端测试了我的REST API,它就是这样工作的.对于第一个请求,我需要传递授权标头.然后就不需要了.我的javascript网络客户端是否也可以这样工作?我打算使用Node.JS和Backbone来构建它.
非常感谢.
EDIT2:似乎真的是一个CORS浏览器问题.我在服务器端添加了标题Access-Control-Allow-Methods for OPTIONS方法,它适用于Chrome.我可以访问JSON响应,不再出错.但是我仍然需要为下一个请求使用授权标头.如何告诉jQuery使用发送的cookie?
当我尝试使用Firefox 11时,我无法访问json响应并且我有错误:
"NetworkError: 401 Non-Autorisé - http://localhost:8087/SpringMVC/users/user1"
Run Code Online (Sandbox Code Playgroud) jquery spring-security cross-domain basic-authentication cors
我正在写使用的PhoneGap(又名科尔多瓦)iOS应用程序,我有一个登录使用一个XMLHttpRequest通过SSL基本身份验证的用户一个简单的HTML登录页面.当您正确输入用户名和密码时,一切都运作良好.但是,如果输入错误的用户名/密码,则不会调用任何回调.
例如,如果您在Chrome上运行相同的代码,使用错误的用户名/密码,则Chrome会以类似的方式运行,但会弹出身份验证质询对话框.在chrome的对话框上点击取消会将控制权返回给我的javascript代码.不幸的是,在iOS上,UIWebView甚至不会弹出一个auth对话框,它只是挂起.我需要一种方法告诉用户他们输入了错误的用户名或密码,以便他们可以重试.
最接近的一个答案,我能找到的这个http://www.freelock.com/2008/06/technical-note-http-auth-with-ajax但是从服务器更改响应状态似乎并不像正确的事情.
这基本上是我的请求代码的样子,但是当发送一个错误的用户名或密码时,它永远不会到达我的onload回调(实际上onreadystatechange回调只被调用一次,那就是readyState 1,也就是OPEN).
var req = new XMLHttpRequest();
req.onload = function(ev) {
if (req.status == 401) {
alert("Invalid Username/Password");
document.getElementById('password').focus();
} else if (req.status == 200) {
window.location.href = some_secure_site;
} else {
// edit //
alert("Some other status");
}
}
req.onerror = function (ev) { alert('Error'); };
req.ontimeout = function(ev) { alert('Timeout'); };
req.open('GET', uri, true, userValue, passValue);
req.withCredentials = true;
req.send();
Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,允许经过身份验证的用户创建OAuth2承载令牌,以便与组织发布的API一起使用.这个想法是允许用户自己生成/撤销这样的令牌,类似于GitHub的Personal API令牌.然后,用户可以使用发布的令牌获得对受保护资源的编程访问.在此配置中,OAuth"客户端","授权服务器"和"资源服务器"属于组织.目前,所有这些服务都驻留在同一个流程中.
为此,我正在尝试支持资源所有者密码凭据授予类型.实施环境包括以下内容:
实现的一个约束是无法访问存储的密码.此处理委托给执行实际身份验证的内部Web服务.
由于无法访问存储密码的限制,因此无法使用默认配置,DaoAuthenticationProvider因为它需要访问UserDetails由提供程序返回的对象提供的密码UserDetailsService.
我的猜测是我需要AuthenticationProvider用自定义实现替换它.但是,所有这样做的尝试似乎都没有生效.
以下似乎在parent引用中正确注册AuthenticationManager,但未在运行时委托(由于DaoAuthenticationProvider优先权):
@Configuration
public class SecurityConfig extends GlobalAuthenticationConfigurerAdapter {
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new AuthenticationProvider() {
@Override
public boolean supports(Class<?> authentication) {
// For testing this is easier, but should check for UsernamePasswordAuthentication.class
return true;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// …Run Code Online (Sandbox Code Playgroud) spring-security basic-authentication spring-boot spring-security-oauth2
我正在尝试使用dbd和mysql作为Apache 2.4的身份验证.
当Apache启动时,我有这个错误:
[Tue May 12 13:07:18.789021 2015] [mpm_event:notice] [pid 10625:tid 140410697815936] AH00489: Apache/2.4.10 (Debian) configured -- resuming normal operations
[Tue May 12 13:07:18.789118 2015] [core:notice] [pid 10625:tid 140410697815936] AH00094: Command line: '/usr/sbin/apache2'
[Tue May 12 13:07:18.789469 2015] [dbd:error] [pid 10628:tid 140410697815936] (20014)Internal error: AH00629: Can't connect to mysql: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
[Tue May 12 13:07:18.789560 2015] [dbd:error] [pid 10628:tid 140410697815936] (20014)Internal error: AH00633: failed to initialise
[Tue May 12 13:07:18.790282 2015] [dbd:error] …Run Code Online (Sandbox Code Playgroud) 我在Java中使用一个非常简单的httpServer来进行GET,POST,PUT和DELETE的api休息.我正在使用基本身份验证,我有几个类Authentication.java和Authorisation.java,我用它来验证和检查用户的权限.
所以,问题是我希望所有用户(经过身份验证)能够从我的api休息中获取信息,但只有具有某些特权的用户才能进行POST,PUT和DELETE.那我该怎么办呢?
这就是我得到的
public class Server {
private static HttpServer server;
public static void start() throws IOException {
server = HttpServer.create(new InetSocketAddress(8000), 0);
HttpContext ctx = server.createContext("/users", new UserHandler());
ctx.setAuthenticator(new ApiRestBasicAuthentication("users"));
server.start();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的ApiRestBasicAuthentication
public class ApiRestBasicAuthentication extends BasicAuthenticator {
private UserAuthentication authentication = new UserAuthentication();
public ApiRestBasicAuthentication(String realm) {
super(realm);
}
@Override
public boolean checkCredentials(String user, String pwd) {
int authCode = authentication.authenticate(user, pwd);
return authCode == UserAuthentication.USER_AUTHENTICATED;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,检查凭据仅检查用户是否经过身份验证.但我想检查一下,如果方法是POST,DELETE或PUT,我还应检查具体的凭据.但是如何在ApiRestBasicAuthentication中获取该方法?我在我的处理程序类中这样做
public void handle(HttpExchange httpExchange) throws IOException { …Run Code Online (Sandbox Code Playgroud) 授权标头不随 HTTP OPTIONS 请求一起发送。我只想在请求为 OPTIONS 时禁用此身份验证,并为其他请求保留此身份验证。这是我目前拥有的相关配置代码。似乎无法理解为什么它不起作用。我总是在 OPTIONS 请求中收到 401 未经授权的错误。
location ~ /foo/bar
{
if ($request_method = OPTIONS) {
set $auth_basic "off";
}
if ($request_method != OPTIONS)
{
set $auth_basic "Resctricted";
set $auth_basic_user_file /var/www/.htpasswd;
}
auth_basic $auth_basic;
auth_basic_user_file $auth_basic_user_file;
}
Run Code Online (Sandbox Code Playgroud) 我们有一个受基本身份验证保护的网络服务器(nginx)https://www.website.com.API https://www.website.com/api没有基本身份验证!
现在的问题是,自Safari 12(macOS和iOS)以来,由我们的javascript应用程序设置的带有/ api请求的承载令牌的http头实际上没有发送到服务器,因为它被替换为基本令牌.我可以在safari开发人员工具的网络选项卡中看到这一点.如前所述/ api是公开的,不受保护!
Chrome例如确实发送了正确的承载令牌,而不是基本令牌.
当我在nginx配置中删除基本身份验证并重新启动safari时,一切正常,并且Bearer令牌被发送到api.
似乎safari 12只是为域的每个请求自动发送基本令牌.
有没有人知道这是否是野生动物园的一个错误?一种解决方案可能是我们将api目标从/ api更改为单独的子域,如api.website.com.
谢谢你的帮助.
safari authorization basic-authentication bearer-token ios12
java ×2
javascript ×2
spring-boot ×2
angularjs ×1
apache ×1
bearer-token ×1
cordova ×1
cors ×1
cross-domain ×1
dbd ×1
groovy ×1
http ×1
httpserver ×1
ios ×1
ios12 ×1
jquery ×1
mysql ×1
nginx ×1
rest ×1
safari ×1
xmlslurper ×1