15
Tôi đang sử dụng thư viện spring-security-javaconfig để bảo mật mùa xuân. Nếu tôi được sử dụng các file xml config, tôi muốn sử dụng một cái gì đó như thế này để xác định một trang tùy chỉnh Access Denied:Làm cách nào để thêm Trình xử lý bị từ chối truy cập trong spring-security-javaconfig
<http auto-config="true">
<intercept-url pattern="/admin*" access="ROLE_ADMIN" />
<access-denied-handler ref="accessDeniedHandler"/>
</http>
Dưới đây là an ninh cấu hình lớp học của tôi cho đến nay:
@Configuration
@EnableWebSecurity
public class SecurityConfigurator extends WebSecurityConfigurerAdapter {
@Override
protected void registerAuthentication(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
auth.inMemoryAuthentication().withUser("admin").password("password").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeUrls().antMatchers("/admin").hasRole("ADMIN");
}
}
Lưu ý rằng việc gọi hàm inMemoryAuthentication() nhiều lần thực sự tạo nhiều phiên bản InMemoryUserDetailsManager. Nếu bạn không muốn chuỗi tất cả mọi thứ, bạn có thể lưu trữ lệnh gọi auth.inMemoryAuthentication() trong một biến. Hoặc bạn có thể sử dụng phương pháp chuỗi như phác thảo trên các ví dụ https://github.com/SpringSource/spring-security-javaconfig/blob/master/samples-web.md#sample-web-security-spring-java-config –