私は自分のプロジェクトで春のセキュリティを使用しています。私はコードからユーザーを保存します。ユーザがログインしてログインページを開こうとした場合、どうすればメインページを開くことができますか?(春のセキュリティ)
@Override
public void saveUser(AuthLkUser lkUser) {
final List<GrantedAuthority> grantedAuths = new ArrayList<>();
GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_USER");
grantedAuths.add(grantedAuthority);
UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(lkUser.getMsisdn(), lkUser.getPricePlan(), grantedAuths);
SecurityContextHolder.getContext().setAuthentication(result);
}
正常に機能します。しかし、succsessufullログイン後、再びログインページを開くことができます。 succsessufullログイン後にオープン可能なログインページを無効にする必要があります。私はこれを試みた:
.antMatchers("/login", "/default/login").access("hasRole('ANONYMOUS')")
しかし、 "ユーザー"の役割を持つユーザーもログインページを開くことができます。
私は
@Autowired
private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;
.successHandler(myAuthenticationSuccessHandler)
しかし、このメソッドが呼び出されないsuccsessufullログイン後の設定へ
@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth) throws IOException, ServletException {
// initialization logic after login
// redirect
HttpSession session = request.getSession();
SavedRequest savedReq = (SavedRequest) session.getAttribute("SAVED_REQUEST");
if (savedReq == null) {
response.sendRedirect(request.getContextPath() + "/landing");
}
else {
response.sendRedirect(savedReq.getRedirectUrl());
}
}
}
とセットアップを試してみました。
ユーザーがログインしてログインページを開こうとした場合、メインページを開くにはどうすればよいですか?
なぜ私のmyAuthenticationSuccessHandler
は呼び出されませんか?
それは、私はあなたがすでにユーザがログインしているかどうかログイン要求をチェックする必要のあるコントローラを追加する必要があると思う私の設定
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/index").access("hasRole('USER')")
// .antMatchers("/login", "/default/login").access("hasRole('ANONYMOUS')")
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/j_spring_security_check")
.successHandler(myAuthenticationSuccessHandler)
.permitAll();
http.csrf()
.disable()
.authorizeRequests()
.antMatchers("/resources/**", "/**").permitAll()
.anyRequest().permitAll()
.and();
}
}
。しかし、私はそれが悪い習慣だと思う – user5620472