春季安全蚂蚁匹配家庭根/ - 春季启动1.4.2发布版本
当用户点击http://localhost:8080时,我有要求通过春季安全显示基于自定义的登录表单(/auth/login.html)。如果用户以管理员角色成功登录,请将用户重定向到/admin/adminsuccess.html。一旦admin用户重定向到/adminsuccess.html,我需要允许admin用户访问其他页面,例如(/admin/assetallocate.html,/admin/assetdeallocate.html..)If用户不具有管理员的角色登录,显示有错误相同的登录页面..春季安全蚂蚁匹配家庭根/ - 春季启动1.4.2发布版本
下面是我的代码:
@Configuration
public class AssetWebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("auth/login");
registry.addViewController("/admin/adminsuccess").setViewName("admin/auth-success");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
@Configuration
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.antMatchers("/**").access("hasRole('ROLE_ADMIN')")
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("username")
.passwordParameter("password")
.defaultSuccessUrl("/admin/adminsuccess")
.and()
.logout()
.logoutSuccessUrl("/login?logout")
.permitAll()
.and()
.csrf().disable();
}
}
/auth/login.html
<form name="loginform" th:action="@{/login}" method="post" class="form-signin">
上面的代码无论我写不符合预期的工作。这可能是蚂蚁匹配模式的问题。请指导。
更新: 当我点击“http://localhost:8080”时,自定义登录页面正在显示。但是,当我输入正确的凭据时,它不会重定向到基于AssetWebConfig.java配置查看名称“/admin/auth-success.html”。如果我输入正确的凭证,下面是当前的回应。
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Nov 23 11:42:59 IST 2016
There was an unexpected error (type=Not Found, status=404).
No message available
是的。问题与你的蚂蚁匹配者。
按我的理解,当你说anyRequest.permitAll,它不符合antMatchers( “/管理/ *”)。访问( “hasRole( 'ROLE_ADMIN')”) 因为你'告诉网络安全,允许所有请求在未经授权的情况下通过。
更改如下,
http.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/admin/**").access("hasRole('ADMIN')")
.and().formLogin().loginPage("/login")
https://github.com/satya-j/rab/blob/master/src/main/java/com/satya/rab/config/WebSecurityConfig.java - 参阅本,其回购我,我早些时候曾在Spring Security尝试了。
编辑:
这里是一个更新
WebSecurityConfig
.antMatchers("/login").permitAll()
.antMatchers("/admin/**").access("hasRole('ADMIN')")
.antMatchers("/**").access("hasRole('USER')")
.and()
.formLogin().loginPage("/login")
.usernameParameter("username")
.passwordParameter("password")
.defaultSuccessUrl("/index")
.failureUrl("/login?error");
您可以使用自己选择的设置基于用户角色的身份验证提供者。
CustomeAuthenticationProvider
@Component("authProvider")
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
String username = auth.getName();
String password = auth.getCredentials().toString();
if(username.equals("user") && password.equals("user")) {
List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
return new UsernamePasswordAuthenticationToken(username, password, grantedAuths);
} else if(username.equals("admin") && password.equals("admin")) {
List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
return new UsernamePasswordAuthenticationToken(username, password, grantedAuths);
} else {
throw new CustomException("Unable to auth against third party systems");
}
}
@Override
public boolean supports(Class<?> auth) {
return auth.equals(UsernamePasswordAuthenticationToken.class);
}
我已经使用了自定义身份验证。当我玩弹簧安全时,我没有去任何数据库配置。你可以用你自己的方式来实现它。以上验证了授权凭证并设置了角色(权限)。由于管理员也可以查看用户模块(大多数情况下,至少这是我的概念),所以当管理员登录时,我已将管理员用户& admin连接起来。简而言之, 1.当用户登录时,他会能够访问每个/ **,但不能访问/ admin/** 2。当管理员日志中,他就可以访问每一个/ **和/管理/ **
我测试过的场景,整个代码,你可以去这里虽然 - https://github.com/satya-j/rab
它不工作: ( – user2057006
你可以发布你的服务器日志或堆栈跟踪,如果你得到一个异常 –
我编辑我的帖子与所需的细节。请帮助。 – user2057006