私はSpringセキュリティとThymeleafでプロジェクトを進めています。私は基本的なSpring Securityの統合を持っています。学生、教授や管理者:SpringセキュリティとThymeleafを使用するロールに応じてリダイレクトするページ -
SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal (AuthenticationManagerBuilder auth) throws Exception
{
auth
.jdbcAuthentication()
.dataSource(dataSource);
}
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/classesTable").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.and()
.httpBasic();
}
}
SecurityWebApplicationInitializer.java
私のプロジェクトでpublic class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer
{
public SecurityWebApplicationInitializer(){
super(SecurityConfig.class);
}
}
私は3つの役割を持っています。 私が達成したいのは、生徒がログインしてindexUser.htmlページにリダイレクトされたときです。教授がログインすると、indexProfesor.htmlにリダイレクトされ、管理者がindexAdmin.htmlページに移動します。
私はこの
if(role.contains("ROLE_ADMIN")){
//redirect from here to indexAdmin.html
}else if(role.contains("ROLE_USER")) {
//redirect to indexUser.html
}else
//redirect to indexProfesor.html
}
のような心の何かに持っていた。しかし、私はどのように全体のコントローラのようになります絵を持っていません。
次のようになります私のhomeContorllerがあります:
@Controller
public class HomeController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginPage(Model model) {
return "login";
}
}
はまた、私はだから私は私のログインユーザーが持っている役割に精通しています私のindex.htmlに
<span sec:authorize="isAuthenticated()">
<span sec:authentication="name"></span>
| Roles: <span sec:authentication="principal.authorities"></span> |
をこのコードを追加しました。 Spring MVCのは、自動的にいくつかのHttpServletRequest
を注入します
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginPage(Model model, HttpServletRequest request) {
if(request.isUserInRole("ROLE_ADMIN") {
// redirect to indexAdmin.html page
} else if(request.isUserInRole("ROLE_USER") {
// redirect indexUser.html page
} else {
// redirect to indexProfesor.html page
}
}
:ここ は、としてあなたのコントローラメソッドを変更することができ、また、私のlogin.htmlとのコード
<form th:action="@{/login}" method="post" class="l-form">
<input type="text" name="username"/>
<input type="password" name="password"/>
<input type="hidden" th:name="${_csrf.parameterName}"
th:value="${_csrf.token}" />
<button type="submit" class="btn">Sign in!</button>
</form>
私たちの答えがあなたに役立つならば、答えをアップアップすることができます。また、あなたはより多くのことに役立つものを受け入れることができます。そして、これは他人の助けを認識する方法です。ありがとうございます – sunkuet02
@ sunkuet02そのノートメイトありがとう! –