基于令牌的身份验证 - 用户将提供其凭据并获得
唯一且有时间限制的访问令牌。我想管理代币
在我自己的实现中创建、检查有效性、过期。
实际上,使用过滤器进行令牌身份验证 - 在这种情况下最好的方法
最终,您可以通过 Spring Data 创建 CRUD 来管理 Token 的属性,例如过期等。
这是我的令牌过滤器:http://pastebin.com/13WWpLq2 http://pastebin.com/13WWpLq2
和令牌服务实施
http://pastebin.com/dUYM555E http://pastebin.com/dUYM555E
一些 REST 资源将是公开的 - 根本不需要进行身份验证
这不是问题,你可以通过 Spring 安全配置来管理你的资源,如下所示:.antMatchers("/rest/blabla/**").permitAll()
某些资源只有具有管理员权限的用户才能访问,
看一眼@Secured
类的注释。例子:
@Controller
@RequestMapping(value = "/adminservice")
@Secured("ROLE_ADMIN")
public class AdminServiceController {
其他资源在所有用户授权后都可以访问。
回到 Spring Security 配置,您可以像这样配置您的 url:
http
.authorizeRequests()
.antMatchers("/openforall/**").permitAll()
.antMatchers("/alsoopen/**").permitAll()
.anyRequest().authenticated()
我不想使用基本身份验证
是的,通过令牌过滤器,您的用户将得到身份验证。
Java代码配置(不是XML)
回到上面的话,看看@EnableWebSecurity
。
你的班级将是:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {}
你必须覆盖配置方法。下面的代码,只是举例说明如何配置匹配器。这是来自另一个项目的。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/assets/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.usernameParameter("j_username")
.passwordParameter("j_password")
.loginPage("/login")
.defaultSuccessUrl("/", true)
.successHandler(customAuthenticationSuccessHandler)
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.invalidateHttpSession(true)
.logoutSuccessUrl("/")
.deleteCookies("JSESSIONID")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.and()
.csrf();
}