在架构设计中采用权限控制机制,限制用户只能访问其拥有权限的数据。可以借助框架或库实现,例如Spring Security、Apache Shiro等。 示例代码:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin*").hasRole("ADMIN")
.antMatchers("/user*").hasRole("USER")
.antMatchers("/common*").permitAll()
.and()
.formLogin().loginPage("/login").defaultSuccessUrl("/common").permitAll()
.and()
.logout().logoutUrl("/logout").permitAll()
.and()
.csrf().disable();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username, password, enabled from user where username=?")
.authoritiesByUsernameQuery("select username, role from user_role where username=?");
}
}
这段代码采用了Spring Security框架,根据用户的角色来授权访问不同的页面,同时使用JDBC实现认证管理,限制了只能访问用户拥有权限的数据。