spring boot security 實現根據情況跳轉不同頁面功能
阿新 • • 發佈:2018-02-23
invalid static request config 隱藏 ctu gif cte send
在配置主類添加代碼
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers(new String[]{"/js/**","/css/**","/picture/**","/images/**","/fonts/**","/**/favicon.ico"}).permitAll() .antMatchers("/home/*").permitAll() .anyRequest().authenticated()View Code// .antMatchers(StaticParams.PATHREGX.NOAUTH,StaticParams.PATHREGX.CSS,StaticParams.PATHREGX.JS,StaticParams.PATHREGX.IMG).permitAll()//無需訪問權限 //.antMatchers(StaticParams.PATHREGX.AUTHADMIN).hasAuthority(StaticParams.USERROLE.ROLE_ADMIN)//admin角色訪問權限 //.antMatchers(StaticParams.PATHREGX.AUTHUSER).hasAuthority(StaticParams.USERROLE.ROLE_USER)//user角色訪問權限 StaticParams自定義枚舉 .and() .formLogin().successHandler(zhu()) //配置過濾器 .loginPage("/login") .failureUrl("/login?error") //.defaultSuccessUrl("/equipment/getIndex", true) .permitAll() .and() .logout() .invalidateHttpSession(true) //是否清除Http session中的內容 .permitAll().and() .csrf() //關閉csrf驗證 .disable(); } @Bean public MyAuthenticationSuccessHandler zhu() { return new MyAuthenticationSuccessHandler(); //自寫的security過濾器 }
新建MyAuthenticationSuccessHandler 實現AuthenticationSuccessHandler接口
/** * * security跳轉過濾器 * @author 蘇俊源 * */ @Component //定義filter類 public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication arg2) throws IOException, ServletException { // TODO Auto-generated method stub String f = request.getParameter("f"); //login前端頁面表單中添加name為f的隱藏字段 if (StringUtils.isNotEmpty(f)) { if(f.equals("su")){ //response.setCharacterEncoding("UTF-8"); //response.getWriter().write("登錄成功123"); response.sendRedirect("/"); } }else{ request.getRequestDispatcher("/").forward(request, response); } }
spring boot security 實現根據情況跳轉不同頁面功能