Xin chào, tôi đã bảo mật một ứng dụng web với bảo mật mùa xuân, với trang đăng nhập. Đây là Cấu hình Bảo mật của tôiOAUTH mùa xuân - đăng nhập khác nhau cho web e REST
@Configuration
@ComponentScan("it.besmart")
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
@Autowired
@Qualifier("customUserDetailsService")
UserDetailsService userDetailsService;
@Autowired
CustomSuccessHandler customSuccessHandler;
@Autowired
CustomAuthenticationFailureHandler customAuthenticationFailureHandler;
@Autowired
DataSource dataSource;
@Autowired
private ConnectionFactoryLocator connectionFactoryLocator;
@Autowired
private UsersConnectionRepository usersConnectionRepository;
@Autowired
private FacebookConnectionSignup facebookConnectionSignup;
private final static Logger logger = LoggerFactory.getLogger(SecurityConfiguration.class);
@Autowired
public void configureGlobalService(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
protected void configure(HttpSecurity http) throws Exception {
logger.debug("Webapp security configured");
http.
authorizeRequests()
.antMatchers("/", "/register", "/registrationConfirm", "/resendRegistrationToken", "/park/**")
.permitAll()
.antMatchers("/edit/**", "/payment/**", "/plate/**", "/book/**", "/home", "/stop/**",
"/notification/**", "/include/**")
.access("hasRole('USER') or hasRole('ADMIN') or hasRole('PARK')").antMatchers("/admin/**")
.access("hasRole('ADMIN') or hasRole('PARK')").antMatchers("/updatePassword")
.hasAuthority("CHANGE_PASSWORD_PRIVILEGE")
.and().formLogin().loginPage("/")
.successHandler(customSuccessHandler).failureHandler(customAuthenticationFailureHandler)
.usernameParameter("email").passwordParameter("password").and().rememberMe()
.rememberMeParameter("remember-me").tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(86400).and().exceptionHandling().accessDeniedPage("/Access_Denied").and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/?logout=true").permitAll();
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public PersistentTokenRepository persistentTokenRepository() {
JdbcTokenRepositoryImpl db = new JdbcTokenRepositoryImpl();
db.setDataSource(dataSource);
return db;
}
}
Điều này hoạt động tốt bằng cách đảm bảo tất cả ứng dụng web của tôi.
Trong cùng một ứng dụng, tôi cũng có một Máy chủ tài nguyên/ủy quyền để bảo vệ một số api REST.
Một số tài nguyên được bảo vệ bằng quyền cấp mã ủy quyền, do đó, Ứng dụng dành cho thiết bị di động không đáng tin cậy sẽ lấy mã thông báo truy cập từ ứng dụng của tôi bằng biểu mẫu đăng nhập. Tôi muốn ứng dụng sử dụng trang đăng nhập khác khi cố gắng đăng nhập từ Ứng dụng dành cho thiết bị di động.
Đây là cấu hình resourceServer tôi
@EnableResourceServer
@ComponentScan("it.besmart.easyparking")
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfig {
private final Logger logger = LoggerFactory.getLogger(ResourceServerConfig.class);
@Autowired
DataSource dataSource;
private static final String RESOURCE_ID = "easyparking_api";
@Configuration
// @Order(2)
public class grantCredentialsConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
logger.debug("Api security configured");
http
.requestMatchers().antMatchers("/api/oauth/**").and().authorizeRequests()
.antMatchers("/api/oauth/**").access("hasRole('USER')").and().formLogin().loginPage("/apilogin")
.permitAll();
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.tokenStore(tokenStore()).resourceId(RESOURCE_ID);
}
}
@Configuration
// @Order(4)
public class clientCredentialsConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
logger.debug("Client security configured");
http
.requestMatchers().antMatchers("/oauth2/**", "/api/registration", "/api/park/**").and()
.authorizeRequests().antMatchers("/oauth2/**", "/api/registration", "/api/park/**").authenticated();
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.tokenStore(tokenStore()).resourceId(RESOURCE_ID);
}
}
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource);
}
}
vậy, grantCredentialsConfiguration
nên chuyển hướng các yêu cầu/form apilogin, nhưng nó không, tôi đang chuyển hướng đến trang đăng nhập ứng dụng web chính ... Làm thế nào nó có thể được hoàn thành?
EDIT
Nhìn gần hơn vào các bản ghi, có vẻ như rằng khi tôi cố gắng để đạt/oauth/ủy quyền/chuỗi bảo mật thông thường diễn ra và tôi nhận được
2017-05-25 12:23:15 DEBUG o.s.security.web.FilterChainProxy[310] - /oauth/authorize?response_type=token&client_id=test&redirect_uri=https://www.getpostman.com/oauth2/callback reached end of additional filter chain; proceeding with original chain
2017-05-25 12:23:15 DEBUG o.s.s.o.p.e.FrameworkEndpointHandlerMapping[310] - Looking up handler method for path /oauth/authorize
2017-05-25 12:23:15 DEBUG o.s.s.o.p.e.FrameworkEndpointHandlerMapping[317] - Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.authorize(java.util.Map<java.lang.String, java.lang.Object>,java.util.Map<java.lang.String, java.lang.String>,org.springframework.web.bind.support.SessionStatus,java.security.Principal)]
2017-05-25 12:23:15 DEBUG o.s.s.w.a.ExceptionTranslationFilter[163] - Authentication exception occurred; redirecting to authentication entry point
org.springframework.security.authentication.InsufficientAuthenticationException: User must be authenticated with Spring Security before authorization can be completed.
Vì vậy, nó trông như tìm kiếm một trình xử lý để quản lý yêu cầu, thay vì chuyển hướng đến/api/apilogin như mong muốn, anh ta tìm thấy một số Authentication exception
và vì vậy tôi truy cập trang đăng nhập chuẩn ... Nhưng tại sao tôi nhận ngoại lệ này?
cảm ơn cho câu trả lời, tôi đã cố gắng, nhưng tôi vẫn tiếp tục được chuyển hướng đến trang đăng nhập ứng dụng web, apilogin không bao giờ được đưa vào tài khoản – besmart