springBoot+springSecurity 动态管理Restful风格权限(三)

1 原文链接:http://blog.csdn.net/u012373815/article/details/55225079

2 链接2:为一个REST服务使用Spring Security的基本和摘要认证

https://my.oschina.net/buwei/blog/193576  

 github https://github.com/eugenp/REST-With-Spring/tree/master/common-client/src/main/java/org/baeldung/client

3 springboot+springsecurity 异步日志 验证码 记住我 (验证xml方式和注解方式事务同时存在的情况)

 https://github.com/facheng/demo/tree/3edf9fe1f22429fb5963e16f40b506fb665dcc00/veen --不是restful

http://blog.csdn.net/pomer_huang/article/details/77902392 -resultful风格

原来默认返回html的类(如跳转到拒绝访问页面),使用自定义的类后,不跳转到html,而是返回给前端状态码。

3和4结合看,但是以3为准,因为3全而且是可运行的完整项目。看对springsecurity的配置在resultful和非resultful风格下有什么区别。

4的部分代码拷贝了一份:

如重写拒绝访问后的逻辑,原来是跳转到拒绝访问页面,现在是返回状态码。

public class GoAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException exception) throws IOException, ServletException {
        response.setHeader("Content-Type", "application/json;charset=utf-8");
        response.getWriter().print("{\"code\":1,\"message\":\""+exception.getMessage()+"\"}");
        response.getWriter().flush();
    }
}

@Configuration
@Import(RootConfig.class)
public class GoWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    //......

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .exceptionHandling()
                    .accessDeniedHandler(new GoAccessDeniedHandler())
                    .authenticationEntryPoint(new GoAuthenticationEntryPoint())
                .and().authorizeRequests()
                    .antMatchers("/", "/csrf").permitAll()
                    .antMatchers("/hello").hasAuthority("ADMIN")
                    .anyRequest().authenticated()
                .and().formLogin()
                    .loginProcessingUrl("/login").permitAll()
                    .successHandler(new GoAuthenticationSuccessHandler())
                    .failureHandler(new GoAuthenticationFailureHandler())
                .and().logout()
                    .logoutUrl("/logout")
                    .logoutSuccessHandler(new GoLogoutSuccessHandler())
                    .invalidateHttpSession(true)
                    .deleteCookies("JSESSIONID")
                .and().requiresChannel()
                    .antMatchers("/pomer").requiresSecure()
                    .anyRequest().requiresInsecure()
                .and().rememberMe()
                    .tokenValiditySeconds(1800)
                    .key("token_key");
    }
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菠萝科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值