spring-mvc中的角色与不同的类别

时间:2017-02-20 19:22:36

标签: java spring spring-mvc spring-boot

如果我有一个超类User,并且类StudentLecturer扩展了这个类,那么在{{1}中使用角色(即安全性)的最佳方法是什么?应用程序。

我觉得几乎没有必要,因为我还有两节课?

3 个答案:

答案 0 :(得分:2)

将类类型用作角色并不是一个好主意,因为它不灵活。假设您创建了一个Student的用户,并将其持久保存到名为student的数据库表中。如果您以后想要将此用户的角色更改为Lecturer,则必须将所有用户数据迁移到新表。

我经常看到使用的一种常见方法是拥有一个包含所有可用角色的中间类(表)。然后每个用户都有一个角色列表(或只有一个角色)。这样,您可以轻松更改适用于不同用户的角色。

例如,假设您有一个基本角色类:

class Role {

    String name;

    // Getters and setters...
}

然后你的学生和讲师课程看起来像这样:

abstract class User {

    List<Role> roles;

    // Easy way to check if a user has a role (better approach would be to use a Set).
    boolean hasRole(String name) {
        for (Role r : roles) {
            if (name.equals(r.getName())) 
                return true
        }
        return false
    }
}

class Lecturer extends User {
}

class Student extends User {
}

这种类型的角色结构有一个明显的好处 - 可以很容易地为数据库中的用户更改角色。

您还可以创建多个小角色。例如,Lecturer可以包含readwritecreate等角色,这将授予他对您的应用程序的所有权利。虽然学生可能只有read角色。

如果您计划对您的应用程序实施某种身份验证,我还建议结帐Spring Security(它也遵循我所描述的类似角色方法)。

答案 1 :(得分:0)

无论如何我会添加安全角色。

处理每个请求时,您不必使用此类。经过身份验证的用户将在会话令牌中“隐藏”他的角色。

我不确定您是否只能使用两个用户类来处理安全问题。

那么未经过身份验证的用户呢?

答案 2 :(得分:0)

如果您想在spring boot应用中设置安全性,可以使用处理角色的spring securtiy并使您更轻松,您还可以创建自己的succes handlers和{{1 class:

首先,在failure handlers

下的pom.xml上添加这些行
dependencies

你必须能够操作 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> 登录自定义表单并不复杂,在定义thymleafthymleaf之后你需要像我一样定义配置类和数据源在下面的代码中

spring security

在定义配置类和数据源之后,您需要定义import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import javax.sql.DataSource ; @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired public void GlobalConfig(AuthenticationManagerBuilder auth,DataSource dataSource) throws Exception { auth.jdbcAuthentication() .dataSource(dataSource) //this is actually where your roles and your user class interferes to login with different roles take this as exemple to your own need .usersByUsernameQuery("SELECT login AS principal , password AS credentials , true FROM utilisateur WHERE login = ? ") .authoritiesByUsernameQuery("SELECT u.login AS principal , r.role as role FROM utilisateur u ,role r where u.id_role=r.id_role AND login = ? "); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring() .antMatchers("/angular/**","/css/**","/js/**","/assets/**"); //this line to make full access of assets } @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .antMatchers("/yourHomePages/ThatDoesn'tIncludeLogin") .permitAll() .anyRequest() .authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .invalidateHttpSession(true) .logoutUrl("/logout") .permitAll(); } } ,因此您必须通过\login这样来解决路径

MvcResolver

import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebMVCconfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login"); } } 是您要创建的自定义登录信息,必须位于viewName

对于任何进一步的信息,您可以使用这些链接 http://www.thymeleaf.org/doc/articles/springsecurity.htmlhttps://github.com/thymeleaf/thymeleafexamples-springsecurity以及春季安全官方文档

相关问题