简体   繁体   English

Spring Security-无法访问数据库

[英]Spring Security - doesn't access database

I am using spring boot and very new to spring security, but I wanted basic security to my web application. 我使用的是Spring Boot,并且是Spring Security的新手,但是我想要Web应用程序的基本安全性。 What I did was add on my pom.xml the needed dependencies and added this java class to my project: 我所做的就是在pom.xml中添加所需的依赖项,并将此java类添加到我的项目中:

@Configuration
@EnableWebSecurity
public class SecurityConfig 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("/**","/event/**","/ticket/**")
            .hasRole("USER")
            .and()
            .formLogin();
}

} }

After running my web application, I run into the login page, where I put user/password and then it goes to my web application. 运行我的Web应用程序后,我进入登录页面,在其中输入用户名/密码,然后转到我的Web应用程序。 However, the commands don't work. 但是,这些命令不起作用。 I am pushing some buttons that should send signals to my MySql database, but nothing happens. 我按了一些应该向MySql数据库发送信号的按钮,但是什么也没有发生。 It's like the front-end isn't connected to the back-end anymore. 就像前端不再连接到后端一样。 I am using AngularJS for front-end and a View Controller that navigates between pages. 我正在使用AngularJS作为前端,以及在页面之间导航的View Controller。 Rest of the application is REST-based. 该应用程序的其余部分基于REST。 Any idea why this might happen? 知道为什么会发生这种情况吗?

Later Edit: Now I understand, the problem that I have is that after authenticating, I get 403 status codes on my end-points. 稍后编辑:现在,我明白了,我遇到的问题是,在进行身份验证之后,我在端点上获得了403状态代码。 Any idea how I might fix it? 知道我该如何解决吗?

Later Editv2: Looks like I don't get authorized on my POST requests, my GET ones work fine...here are some of my POST end-points: /event/buy_ticket/{id} , /ticket//cancel_ticket/{id} 以后的Editv2:看起来我未获得POST请求的授权,我的GET请求工作正常...这是我的一些POST端点:/ event / buy_ticket / {id},/ ticket // cancel_ticket / { ID}

angular.min.js:101 POST http://localhost:8080/event/buy_ticket/2 403 ()

I even tried to explicitly say it to permit it, but I still get 403... 我什至试图明确地说出它允许它,但我仍然得到403 ...

http.authorizeRequests()

.antMatchers("/**","/event/**","/ticket/**","/event/buy_ticket/2")
            .permitAll()
            .and()
            .formLogin();

Later later later edit: 以后以后以后编辑:

Disabling csrf worked 禁用CSRF工作

Getting 403 Forbidden error codes means that Spring is receiving your requests but choosing to stop processing them. 得到403 Forbidden错误代码意味着Spring正在接收您的请求,但选择停止处理它们。 From the Wiki page on HTTP 403 : HTTP 403的Wiki页面上:

Authentication was provided, but the authenticated user is not permitted to perform the requested operation. 提供了身份验证,但是不允许经过身份验证的用户执行请求的操作。

If I had to wager, I would say the problem is that you have not specified what resources and endpoints should be accessible and how. 如果不得不下注,我会说问题是您尚未指定应访问哪些资源和端点以及如何访问。 If memory serves me right, Spring Security will, by default, lock down everything super tightly so you need to explicitly tell it what to leave open. 如果内存适合我,那么默认情况下,Spring Security会非常紧密地锁定所有内容,因此您需要明确告诉它保持打开状态。 Below is a sample from my own security configuration: 以下是我自己的安全配置的示例:

@Override
protected void configure(HttpSecurity http) throws Exception {    

  http
     .authorizeRequests() // require authorization
     .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() // for the CORS preflight check
     .antMatchers("/login", "/api/open/**", "/resources/**").permitAll()  // the open API endpoints and resources
     .antMatchers("/logout", "/api/secured/**").authenticated(); // lock down these endpoints

  ...additional configurations...
}

All endpoints that should be freely available are prefaced with "/api/open/" while all endpoints that should be protected by Spring Security are prefaced with "/api/secured/" . 应该自由可用的所有端点都以"/api/open/"开头,而应该由Spring Security保护的所有端点都以"/api/secured/"开头。 The exceptions are the logout and login endpoints, since those tie into Spring Security directly. 登出和登录端点是例外,因为它们直接绑定到Spring Security。

Here's a great blog post - and the related repo - that shows off how to implement Spring Security that plays nice with AngularJS, even as a Single Page Application, which are notoriously annoying to secure. 这是一篇很棒的博客文章 -及其相关的仓库 -展示了如何实现与AngularJS配合使用的Spring Security,甚至作为单页应用程序也是如此,这是众所周知的烦人的安全方法。

Edit: You might be running up against CSRF protection, which is enabled by default in Spring Security - see this post . 编辑:您可能正在使用CSRF保护,该保护在Spring Security中默认启用-请参阅此文章 CSRF will allow HTTP "safe" methods like GET, HEAD, and OPTION but block "unsafe" methods like PUT, POST, DELETE, etc if those methods do not provide the proper token (since no CSRF was configured, those request don't have a token -> blocked). CSRF将允许HTTP“安全”方法(例如GET,HEAD和OPTION),但会阻止“不安全”方法(例如PUT,POST,DELETE等),如果这些方法未提供正确的令牌(由于未配置CSRF,则这些请求不会拥有令牌->被阻止)。 For testing purposes, you can disable it by adding http.csrf().disable() to the configure() method. 为了进行测试,您可以通过将http.csrf().disable()configure()方法中来禁用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring MVC:Spring Security 4不拦截 - Spring MVC: Spring Security 4 doesn't intercept jdbcAuthentication()而不是inMemoryAuthentication()不提供访问权限 - Spring Security和Spring Data JPA - jdbcAuthentication() instead of inMemoryAuthentication() doesn't give access - Spring Security and Spring Data JPA spring 用于访问令牌实现的安全 openid 连接请求与 CAS api 要求不一致 - spring security openid connect request for access token implementation doesn't align cas api requirement Spring 安全性:拒绝访问处理程序不起作用(xml 配置 + 控制器方法上的预授权注释) - Spring security : Access denied handler doesn't work (xml config + preauthorize annotation on controller method) 为什么isAuthenticated()可以工作,而access =“ hasRole('VERIFIED')”不能在Spring Security项目中工作? - Why does isAuthenticated() works, but not access=“hasRole('VERIFIED')” doesn't work in Spring Security project? CrossOrigin注释不适用于Spring Security - CrossOrigin annotation doesn't work with spring security 春季安全+休息不起作用 - spring security + rest doesn't work 使用 Spring 保护应用程序安全不起作用 - Securing app with Spring Security doesn't work Java Spring 安全hasAnyAuthority不起作用 - Java Spring Security hasAnyAuthority doesn't works Spring Security禁用rememberMe不起作用 - Spring Security disable rememberMe doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM