Dmi*_*y S 2 spring-security spring-boot
当我尝试在管理员授权后添加新帖子时,我看到此响应.
我有基于Spring启动安全性的基本授权:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
//...declared fields
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("user")
.password("userpass")
.roles("USER")
.and()
.withUser("admin")
.password("adminpass")
.roles("ADMIN", "USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/logout").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic()
.and().logout().permitAll()
.and()
.formLogin()
.loginProcessingUrl("/login")
.permitAll()
.and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Run Code Online (Sandbox Code Playgroud)
尝试在授权后添加新帖子时收到此消息:
{
"timestamp": "2018-07-04T12:19:25.638+0000",
"status": 403,
"error": "Forbidden",
"message": "Forbidden",
"path": "/post/create"
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中:
@RestController
public class PostController {
@Autowired
private PostDAO postDAO;
@GetMapping("/posts")
public Page<Post> getAllPosts(Pageable pageable) {
return postDAO.findAll(pageable);
}
@PostMapping("/post/create")
public Post createPost(@Valid @RequestBody Post post) {
return postDAO.save(post);
}
//other end-points........
}
Run Code Online (Sandbox Code Playgroud)
但是,从我的控制器读取操作运行良好,但我无法访问CRUD操作.
有我的依赖:
dependencies {
compile ('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.hibernate:hibernate-core')
compile('org.springframework.boot:spring-boot-starter-security')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
testCompile('junit:junit')
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?提前致谢!
这是由于CSRF启用.CSRF默认情况下,Java配置中启用了保护.我们仍然可以CSRF使用下面给出的配置禁用.
http .csrf().disable() .authorizeRequests() .anyRequest().permitAll();
Run Code Online (Sandbox Code Playgroud)
从Spring Security 4.x开始 - CSRF默认情况下,XML配置中也启用了保护; 如果我们需要,我们当然可以禁用它:
<http>
...
<csrf disabled="true"/>
</http>
Run Code Online (Sandbox Code Playgroud)
注意:CSRF是一种攻击,它会强制最终用户在当前已通过身份验证的Web应用程序中执行不需要的操作.
| 归档时间: |
|
| 查看次数: |
2543 次 |
| 最近记录: |