如何通过使用Spring的OAuth2来保护MVC应用程序? [英] How to secure a MVC application with OAuth2 using Spring?
问题描述
对不起,我的英语。
我有一个可以按常规方式登录的应用程序。
@Configuration
@EnableWebSecurity
public class LoginSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
System.out.println("LoginSecurityConfig :: configure");
auth.jdbcAuthentication().dataSource( getDataSource() )
.passwordEncoder( new BCryptPasswordEncoder(16) )
.usersByUsernameQuery(
"select user_name as username,password,enabled from users where user_name=?")
.authoritiesByUsernameQuery(
"select user_name as username, role_name from users_roles ur join users u on ur.user_id = u.user_id and u.user_name = ?");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/login*").anonymous()
.antMatchers("/resources/**").permitAll()
.antMatchers("/fotos/**").permitAll()
.antMatchers("/users").access("hasRole('ROLE_ADMIN')")
.antMatchers("/user").access("hasRole('ROLE_ADMIN')")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/loginPage")
.defaultSuccessUrl("/home", true)
.failureUrl("/loginPage?error=true")
.loginProcessingUrl("/login")
.usernameParameter("username")
.passwordParameter("password")
.and()
.logout()
.logoutSuccessUrl("/loginPage")
.invalidateHttpSession(true);
}
}
使用它,我可以尝试访问任何受保护的资源,系统会将我发送到username
和password
到内部login
控制器,然后我就有了Principal
并可以访问受保护的资源(主目录、用户、用户)。工作正常。
但是...我需要删除用户控制数据库,并使用OAuth2来允许相同类型的访问。我不想在我的数据库中再有任何用户。我需要一个登录屏幕,然后像http://myserver/oauth/token?grant_type=password&username=admin&password=admin
在Basic
中传递client_id
和client_secret
这样的令牌请求。我知道如何做"获取令牌"部分,我的服务器工作得很好,给了我令牌和刷新令牌,但只使用了Postman,因为我不知道如何在我的Web应用程序代码中使用它。我找到的所有教程都在同一应用程序中同时使用服务器和客户端,实际上并没有展示如何使用OAuth2远程服务器。
已尝试使用this。这是一个很好的教程,非常接近我所需要的,但对我来说太复杂了。
我有这个代码,我知道它可以使用服务器并使用客户端凭据发布令牌,但不知道如何给用户一个登录屏幕并获取他的凭据来完成请求(GET
部分)。
@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfigRemoteTokenService extends ResourceServerConfigurerAdapter {
@Override
public void configure(final HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and()
.authorizeRequests().anyRequest().permitAll();
}
@Primary
@Bean
public RemoteTokenServices tokenServices() {
final RemoteTokenServices tokenService = new RemoteTokenServices();
tokenService.setCheckTokenEndpointUrl("http://myoauthserver/oauth/check_token");
tokenService.setClientId("clientid");
tokenService.setClientSecret("password");
return tokenService;
}
}
所以...如何保护我的系统,从用户那里获取登录和密码,并使用此代码来控制凭据,就像我使用通常的数据库方法一样?
或OAuth2仅用于安全REST API?
请对新手友好,因为我不太习惯使用Spring。
推荐答案
简单如1,2,3...
只需将My OAuth2服务器稍作更改即可接受oauth/authorize
方法。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/login", "/oauth/authorize")
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.permitAll();
}
并创建自定义登录表单。现在,所有客户端(Web应用程序)都可以登录到它。
您可以在此处找到示例客户端和更多详细信息:http://www.baeldung.com/sso-spring-security-oauth2
您还可以在我的GitHub存储库查看我的整个服务器和客户端:
https://github.com/icemagno/geoinfra/cerberus
和
https://github.com/icemagno/geoinfra/atlas
在pt_BR中
这篇关于如何通过使用Spring的OAuth2来保护MVC应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!