在Grails(3.1.9版)中,我试图找到一种方法将用户登录后重定向到其他页面,而不是现在。我以前没有使用过该应用程序,但是从我注意到的情况来看,它使用的是SpringSecurity和SAML,到目前为止我从未使用过。这是包含登录表单的auth.gsp代码:
<form action="/login/authenticate" method="POST" id="loginForm" class="cssform"
autocomplete="off">
<label for="username"><g:message code='springSecurity.login.username.label'/>:</label>
<div class="pg-input">
<input type="text" name="${usernameParameter ?: 'username'}" id="username"/>
</div>
<label for="password"><g:message code='springSecurity.login.password.label'/>:</label>
<div class="pg-input">
<input type="password" name="${passwordParameter ?: 'password'}" id="password"/>
</div>
<div class="pg-row">
<button class="pg-button square" role="button" type="submit" id="submit"
value="${message(code: 'springSecurity.login.button')}"
>Log in</button>
</div>
</form>
我对此并不陌生,但是如果我是正确的话,
action="/login/authenticate"
是指SpringSecurity。这是Config.groovy类代码的某些部分:
@Configuration
@EnableWebSecurity
class Config extends WebSecurityConfigurerAdapter {
...
// Handler deciding where to redirect user after successful login
@Bean
SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler() {
SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler =
new SavedRequestAwareAuthenticationSuccessHandler()
successRedirectHandler.setDefaultTargetUrl("/content")
successRedirectHandler.setAlwaysUseDefaultTargetUrl(true)
return successRedirectHandler
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(delegatingEntryPoint())
http
.httpBasic()
.authenticationEntryPoint(samlEntryPoint())
http
.csrf()
.disable()
http.headers().frameOptions().disable()
http
.addFilterBefore(metadataGeneratorFilter(), ChannelProcessingFilter.class)
.addFilterAfter(samlFilter(), BasicAuthenticationFilter.class)
http
.authorizeRequests()
.antMatchers('/admin/**').hasRole('ROLE_ADMIN')
.antMatchers('/panel').hasRole('ROLE_ADMIN')
.antMatchers('/index/**').hasRole('ROLE_ADMIN')
.antMatchers('/admin/**').hasRole('ROLE_ADMIN')
.antMatchers('/item/**').hasRole('ROLE_ADMIN')
.antMatchers('/category/**').hasRole('ROLE_ADMIN')
.antMatchers('/serviceUser/**').hasRole('ROLE_ADMIN')
.antMatchers('/serviceRole/**').hasRole('ROLE_ADMIN')
.antMatchers('/fileLink/**').hasRole('ROLE_ADMIN')
.antMatchers('/serviceUserServiceRole/**').hasRole('ROLE_ADMIN')
.antMatchers("/error").hasRole('ROLE_ADMIN')
.antMatchers("/content").fullyAuthenticated()
.antMatchers("/content/**").fullyAuthenticated()
.antMatchers("/assets/files/**").fullyAuthenticated()
.antMatchers("/**").permitAll()
.antMatchers("/assets/**").permitAll()
.antMatchers("/saml/**").permitAll()
.anyRequest().authenticated()
.anyRequest().hasAnyRole('ROLE_USER', 'ROLE_ADMIN')
http
.logout()
.invalidateHttpSession(true)
.logoutSuccessUrl("/")
}
到目前为止,我已经尝试过:
successRedirectHandler()
方法中的setDefaultTargetUrl("/content")
更改为setDefaultTargetUrl("https://www.google.com")
,将setAlwaysUseDefaultTargetUrl(true)
更改为setAlwaysUseDefaultTargetUrl(false)
configure(HttpSecurity http)
方法中的http.loginForm().defaultSuccessUrl("https://www.google.com", true)
目前,在用户登录后,应用程序从http://localhost:443/login/auth重定向到http://localhost:443/content/index。我需要找到指定此路径的位置,但是我再也没有想法了。我也认为它可能与SAML有关,但是目前我不知道如何使用它,而从头开始学习它可能需要一些时间。告诉我是否应该添加更多信息,因为我不确定在此输入的内容是否足够。
最佳答案
令人难以置信的是,经过数小时的研究,我如何发布问题并在发现后立即找到答案。
我发现在application.yml中指定了一个属性-successHandler.defaultTargetUrl:'/ content / index'
更改后,该应用程序将按我的要求工作。似乎application.yml属性会覆盖其他文件中指定的所有内容。
关于authentication - 登录后Grails 3重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49381819/