亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用spring security成功登錄后如何將對象添加到視圖中?

使用spring security成功登錄后如何將對象添加到視圖中?

料青山看我應如是 2023-08-16 17:49:37
成功登錄后,我嘗試重定向到需要實例化對象的頁面,如我的 HomeController 中所述:@RequestMapping(value={"/","/home"}, method=RequestMethod.GET)public ModelAndView home() {    ModelAndView view = new ModelAndView("home");    view.addObject("client", new Client());    return view;}問題是我不知道如何使用 spring security 來執行此操作,因為我能做的唯一設置是在成功登錄后設置頁面:.formLogin()    .loginPage("/login")    .defaultSuccessUrl("/home")    .permitAll()使用 spring security 成功登錄后如何將此對象添加到視圖中?
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

假設您有這樣的 WebSecurity 配置。你只需要添加一個successHandler


@Configuration

@EnableWebSecurity

public class SecSecurityConfig extends WebSecurityConfigurerAdapter {



? ? @Autowired

? ? private SimpleAuthenticationSuccessHandler successHandler;


? ? @Bean("authenticationManager")

? ? @Override

? ? public AuthenticationManager authenticationManagerBean() throws Exception {

? ? ? ? ? ? return super.authenticationManagerBean();

? ? }


? ? @Autowired

? ? public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

? ? ? ? // @formatter:off

? ? ? ? auth.inMemoryAuthentication()

? ? ? ? ? ? .withUser("user1").password("{noop}user1Pass").roles("USER")

? ? ? ? ? ? .and()

? ? ? ? ? ? .withUser("admin1").password("{noop}admin1Pass").roles("ADMIN");

? ? ? ? // @formatter:on

? ? }



? ? @Override

? ? protected void configure(final HttpSecurity http) throws Exception {

? ? ? ? http.authorizeRequests()

? ? ? ? ? ? .antMatchers("/anonymous*").anonymous()

? ? ? ? ? ? .antMatchers("/login*").permitAll()

? ? ? ? ? ? .anyRequest().authenticated()


? ? ? ? ? ? .and()

? ? ? ? ? ? .formLogin()

? ? ? ? ? ? .loginPage("/login.html")

? ? ? ? ? ? .loginProcessingUrl("/login")

? ? ? ? ? ? .successHandler(successHandler)

? ? ? ? ? ? // ...? ? ? ??

? ? }

}

SimpleAuthenticationSuccessHandler 類


// Change onAuthenticationSuccess logic as per your requirement


@Component

public class SimpleAuthenticationSuccessHandler implements AuthenticationSuccessHandler {


? ? private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();


? ? @Override

? ? public void onAuthenticationSuccess(HttpServletRequest arg0, HttpServletResponse arg1, Authentication authentication)

? ? ? ? ? ? throws IOException, ServletException {



? ? ? ? redirectStrategy.sendRedirect(arg0, arg1, "/home");



? ? ? ? /*

? ? ? ? Collectionextends GrantedAuthority> authorities = authentication.getAuthorities();

? ? ? ? authorities.forEach(authority -> {

? ? ? ? ? ? if(authority.getAuthority().equals("ROLE_USER")) {

? ? ? ? ? ? ? ? try {

? ? ? ? ? ? ? ? ? ? redirectStrategy.sendRedirect(arg0, arg1, "/user");

? ? ? ? ? ? ? ? } catch (Exception e) {

? ? ? ? ? ? ? ? ? ? // TODO Auto-generated catch block

? ? ? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? } else if(authority.getAuthority().equals("ROLE_ADMIN")) {

? ? ? ? ? ? ? ? try {

? ? ? ? ? ? ? ? ? ? redirectStrategy.sendRedirect(arg0, arg1, "/admin");

? ? ? ? ? ? ? ? } catch (Exception e) {

? ? ? ? ? ? ? ? ? ? // TODO Auto-generated catch block

? ? ? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? throw new IllegalStateException();

? ? ? ? ? ? }

? ? ? ? });


? ? ? ? */



? ? }


}

這會將您的調用重定向到"/home",控制器將進一步負責加載您的對象。

查看完整回答
反對 回復 2023-08-16
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號