【问题标题】:Spring 3.2: Choose view basing on user roleSpring 3.2:根据用户角色选择视图
【发布时间】:2014-01-31 11:07:00
【问题描述】:

使用 3.2 版的 spring-webmvcspring-security-web,我想根据用户角色(或用户是否经过身份验证)返回不同的视图与否),因此对于 "/" 请求,角色 ANONYMOUS 的用户(或未经过身份验证的用户)获得 welcome 页面和角色 USER 获取 主页 页面。

我目前的方法是使用常规控制器:

@Controller
public class WelcomeCtrl {

    @RequestMapping("/")
    public String welcome(Principal principal) {
        if (userAuthenticated(principal)) {
            return "redirect:home";
        }
        return "welcome";
    }

    private boolean userAuthenticated(Principal principal) {
        return principal != null && principal instanceof Authentication
                && hasUserRole((Authentication) principal);
    }

    private boolean hasUserRole(Authentication principal) {
        Collection<? extends GrantedAuthority> authorities = (principal)
                .getAuthorities();
        return Iterables.contains(Collections2.transform(authorities,
                new Function<GrantedAuthority, String>() {

                    @Override
                    public String apply(GrantedAuthority authority) {
                        return authority.getAuthority();
                    }
                }), "ROLE_USER");
    }

}

但是,我不是很喜欢它,因为我觉得这个重定向应该用 spring security 来完成(我错了吗?)。您知道使用 Spring Security 配置执行此操作的任何方法吗?我目前的配置如下:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/welcome").permitAll()
                .anyRequest().authenticated()
            .and().formLogin()
                .defaultSuccessUrl("/home").permitAll()
                .and().logout().permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder authManagerBuilder)
            throws Exception {
        authManagerBuilder.inMemoryAuthentication().withUser("user")
                .password("password").roles("USER");
    }
}

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

    在我看来,这是使用市场上最新的 spring 安全配置的最佳方式。

    在 Spring 3.1 中向 Spring 框架添加了对 Java 配置的一般支持。自 Spring Security 3.2 起,Spring Security Java 配置支持使用户无需使用任何 XML 即可轻松配置 Spring Security。

    如果您熟悉安全命名空间配置,那么您应该会发现它与安全 Java 配置支持之间有很多相似之处,并且您可以使用安全命名空间配置以及适合您的套件

    spring security 3.2 中添加了许多新东西,你可以使用它 喜欢

    1)。 Java Configuration -- 更适合那些更熟悉阅读 jar 文件以应用配置方法的人。

    2)。并发支持——在大多数环境中,安全性存储在每个线程的基础上。这意味着当在新线程上完成工作时,SecurityContext 会丢失。 Spring Security 提供了一些基础设施来帮助用户更轻松地完成此操作。 Spring Security 为在多线程环境中使用 Spring Security 提供了低级抽象。事实上,这就是 Spring Security 与 AsyncContext.start(Runnable) 和 Spring MVC Async Integration 集成的基础。

    3)。 CSRF 攻击:- 假设您的银行网站提供了一个表单,允许将资金从当前登录的用户转移到另一个银行账户。例如,HTTP 请求可能如下所示:

    POST /transfer HTTP/1.1
    Host: bank.example.com
    Cookie: JSESSIONID=randomid; Domain=bank.abc.com; Secure; HttpOnly
    Content-Type: application/x-www-form-urlencoded
    
    amount=100.00&routingNumber=1234&account=9876
    

    现在假装您对银行网站进行了身份验证,然后在不退出的情况下访问恶意网站。邪恶网站包含一个 HTML 页面,格式如下:

    <form action="https://bank.example.com/transfer" method="post">
      <input type="hidden"
          name="amount"
          value="100.00"/>
      <input type="hidden"
          name="account"
          value="evilsAccountNumber"/>
      <input type="submit"
          value="ohh you Win Money!"/>
    </form>
    

    您喜欢赢钱,所以您点击提交按钮。在此过程中,您无意中将 100 美元转给了恶意用户。发生这种情况是因为,虽然恶意网站无法看到您的 cookie,但与您的银行关联的 cookie 仍会随请求一起发送。

    使用 spring security 3.2 可以轻松阻止此类攻击,因为 spring security 中的这个新功能 Cross Site Request Forgery (CSRF) Protection 会生成一些在付款时匹配的令牌网关端。

    还有更多优势............

    【讨论】:

    • @Macias 你对我的回答满意吗,朋友你觉得好看吗
    • @Macias 如果你觉得不错,请点赞,这样对朋友会有帮助
    • 那么在这个来自 spring 文档的复制粘贴中究竟在哪里可以回答我的问题? -1 我的朋友
    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 2017-06-01
    • 2014-02-05
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2018-08-20
    相关资源
    最近更新 更多