我正在按照auth0的教程使用JWT保护我的应用程序。
我完成了以下WebSecurity配置:
@EnableWebSecurity
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class WebSecurity extends WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService;
private final BCryptPasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.and().cors()
.and().csrf()
.disable()
.authorizeRequests()
.antMatchers(HttpMethod.POST, REGISTER_URL).permitAll()
.antMatchers(HttpMethod.POST, LOGIN_URL).permitAll()
.anyRequest().authenticated()
.and()
.addFilter(new JWTAuthorizationFilter(authenticationManager()))
.addFilter(new JWTAuthenticationFilter(authenticationManager()))
// This disables session creation on Spring Security
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
}
和以下JWTAuthenticationFilter:
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
private final AuthenticationManager authenticationManager;
public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
try {
ApplicationUser credentials = new ObjectMapper().readValue(request.getInputStream(), ApplicationUser.class);
return authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
credentials.getUsername(),
credentials.getPassword(),
new ArrayList<>()
)
);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
String token = Jwts.builder()
.setSubject(((User) authResult.getPrincipal()).getUsername())
.setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
.signWith(SignatureAlgorithm.HS512, SECRET.getBytes())
.compact();
response.addHeader(HEADER_STRING, TOKEN_PREFIX + token);
}
}
目前,该应用接受/login
URL
上的POST请求。我想知道如何将URL更改为/api/auth/login
。有什么方法可以将URL字符串注入到身份验证过滤器中,或以某种方式在安全配置中对其进行设置?
您正在扩展org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter,它本身又扩展了
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter。在最后一节课中,有一个名为setter的操作setFilterProcessesUrl
,它旨在执行以下操作:
setFilterProcessesUrl
public void setFilterProcessesUrl (字符串filterProcessesUrl)
设置确定是否需要身份验证的URL
参数:filterProcessesUrl
这是该javadoc部分的链接
因此,您WebSecurityConfigurerAdapter
可以像这样:
@Bean
public JWTAuthenticationFilter getJWTAuthenticationFilter() {
final JWTAuthenticationFilter filter = new JWTAuthenticationFilter(authenticationManager());
filter.setFilterProcessesUrl("/api/auth/login");
return filter;
}
然后在configure
同一个类的方法中,只需引用它即可,而不是创建新实例:
.addFilter(getJWTAuthenticationFilter())
问题内容: 因此,我目前正在运行已安装ldapjs的node.js。 我的目标是拥有一个使用ldapjs的系统,以允许用户使用用户名和密码登录。 我已经阅读了一段时间的http://ldapjs.org文档,但是正在努力理解ldap的整个思想以及ldapjs的实现。 我目前在文档中有这个 这使我可以运行以下内容并成功绑定到服务器。 但是我真的不确定从这里走到哪里,即使这是正确的方法… 理想的设置是
使用,https://github.com/firebase/FirebaseUI-Android/tree/master/codelabs/chat作为登录的参考,我在键入时似乎遇到了问题 我只能键入Auth的提供者,而不能键入,为什么会这样,它提示我键入社会提供者。
我正在尝试按照以下准则使用谷歌的新火力基础 sdk 设置自定义身份验证: https://firebase.google.com/docs/auth/server#use_a_jwt_library 在 samble 代码中它说: 从JSON密钥文件中获取您的服务帐户的电子邮件地址和私钥 不幸的是,我不知道从哪里获取这个json文件。如果我去我的firebase控制台(https://consol
我们希望使用外部身份提供者将现有的Spring Security项目从自定义用户名/密码实现(UserDetailsService等)迁移到oauth2登录。 但是,当通过外部提供程序登录时,安全上下文中的身份验证对象是一个,主体是一个。 由于我们在应用程序中使用的都是自定义身份验证主体,因此我们希望将转换为自定义对象。 对于oauth2资源服务器,似乎有一个API可让您将JWtAuthentia
我有一个角度7前端和Spring后端。 我的目标是使用JSON表单进行自定义登录。计划是:以json格式将用户名/密码从前端发送到后端。在后端进行身份验证,并将结果发送回前端。我的问题:从前端发送数据后,我在后端看不到数据(用户名和密码为空)(在我的CustomAuthenticationProvider.java中) 我的登录页面功能: 当我在用户名和密码中添加值后打印出时,我得到这个(主体和凭
问题内容: 这是我的情况: 一个Web应用程序对许多应用程序执行某种SSO 登录的用户,而不是单击链接,该应用就会向正确的应用发布包含用户信息(名称,pwd [无用],角色)的帖子 我正在其中一个应用程序上实现SpringSecurity以从其功能中受益(会话中的权限,其类提供的方法等) 因此,我需要开发一个 自定义过滤器 -我猜想-能够从请求中检索用户信息,通过自定义 DetailsUserSe