[英]spring boot and spring security custom login page and controller
我想通过自定义控制器和登录页面对登录和退出进行更多控制。
我的SecurityConfiguration代码当前如下所示:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private SpringDataJpaUserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(this.userDetailsService)
.passwordEncoder(Manager.PASSWORD_ENCODER);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**", "/built/**", "/main.css", "/login.css").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/loginSecure")
.defaultSuccessUrl("/index", true)
.permitAll()
.usernameParameter("username").passwordParameter("password")
.and()
.csrf().disable()
.logout()
.permitAll();
}
}
我在Controller中的登录配置:
@RequestMapping(value = "/login")
public String login() {
return "login";
}
我的loginSecure在控制器中的映射:
@RequestMapping(value="/loginSecure", method = RequestMethod.POST)
public String login(@RequestAttribute("username") String userName, @RequestAttribute("password") String password) {
//does the authentication
final Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
userName,
password
)
);
SecurityContextHolder.getContext().setAuthentication(authentication);
return "index";
}
我的login.html:
<form class="login100-form validate-form" action="/loginSecure" method="post">
<span class="login100-form-title p-b-26">
Welcome
</span>
<span class="login100-form-title p-b-48">
<i class="zmdi zmdi-font"></i>
</span>
<div class="wrap-input100 validate-input" data-validate = "Valid email is: a@b.c">
<input class="input100" type="text" id="username" name="username"/>
<span class="focus-input100" data-placeholder="Email/Username"></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Enter password">
<span class="btn-show-pass">
<i class="zmdi zmdi-eye"></i>
</span>
<input class="input100" type="password" id="password" name="password"/>
<span class="focus-input100" data-placeholder="Password"></span>
</div>
<div class="container-login100-form-btn">
<div class="wrap-login100-form-btn">
<div class="login100-form-bgbtn"></div>
<button class="login100-form-btn">
Login
</button>
</div>
</div>
</form>
当我提交表单时,在chrome开发工具中,它提交为loginSecure? 使用url编码,但是它只是再次重定向回login.html。
编辑:从login.html中删除了多余的形式,并向securityConfiguration添加了csfr()。disable。 将loginProcessUrl添加到httpSecurity并对其进行了修复。 上面的代码有效。
从您写的内容来看,我想问题是在单击“登录”后,您的应用程序被两个请求击中。
我认为问题在于您的登录页面具有两种形式。 因此,当您单击“登录”时,两个表单都会发送其请求。 您可以在Chrome开发者工具中进行验证。
如您在这里阅读的HTML不允许使用嵌套表单在另一个html表单中包含html表单是否有效?
如果创建自定义登录html和自定义身份验证器,则需要将其添加到HttpSecurity配置-> .loginProcessingUrl(“ / loginSecure”)
这里的好例子-> https://www.boraji.com/spring-security-4-custom-login-from-example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.