带有thymeleaf的Java spring引导登录示例代码
最近我发现了一个在SpringBoot中简单注册和登录的例子。 代码可在此处找到: 但是,我不能在我的项目中使用thymeleaf,我需要该项目的功能-我的问题是: 登录在代码中是如何处理的?如果我删除maven登录中的thymeleaf依赖项,则无法工作。如果我们查看登录控制器内部:带有thymeleaf的Java spring引导登录示例代码,java,spring-boot,thymeleaf,Java,Spring Boot,Thymeleaf,最近我发现了一个在SpringBoot中简单注册和登录的例子。 代码可在此处找到: 但是,我不能在我的项目中使用thymeleaf,我需要该项目的功能-我的问题是: 登录在代码中是如何处理的?如果我删除maven登录中的thymeleaf依赖项,则无法工作。如果我们查看登录控制器内部: @Controller public class IndexController { @GetMapping("/") public String showIndexPage() { return
@Controller
public class IndexController {
@GetMapping("/")
public String showIndexPage() {
return "index";
}
@GetMapping("/login")
public String showLoginForm() {
return "views/loginForm";
}
}
没有
密码和登录
验证!这是怎么回事?我只是想在不使用thymeleaf的情况下完成这个项目您需要使用任何视图技术而不是thymeleaf来生成HTML表单:
p.S.您也可以使用(请参阅)在Java代码中生成HTML表单,但生成HTML表单的方法非常古老且奇怪,我不推荐使用 您需要使用任何视图技术而不是thymeleaf来生成HTML表单:
p.S.您也可以使用(请参阅)在Java代码中生成HTML表单,但生成HTML表单的方法非常古老且奇怪,我不推荐使用 您可以检查 在这些行中,他/她实现了登录逻辑:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select email as principal, password as credentails, true from user where email=?")
.authoritiesByUsernameQuery("select user_email as principal, role_name as role from user_roles where user_email=?")
.passwordEncoder(passwordEncoder()).rolePrefix("ROLE_");
}
他/她实现了这些行来指定提交的用户凭据
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/register", "/", "/about", "/login", "/css/**", "/webjars/**").permitAll()
.antMatchers("/profile").hasAnyRole("USER,ADMIN")
.antMatchers("/users","/addTask").hasRole("ADMIN")
.and().formLogin().loginPage("/login").permitAll()
.defaultSuccessUrl("/profile").and().logout().logoutSuccessUrl("/login");
}
更具体地说,.formLogin().loginPage(“/login”).permitAll()
最后,让我们开始处理您的问题。这与我们无关。您需要创建提交到/login
的登录页面
关于的登录逻辑您可以检查 在这些行中,他/她实现了登录逻辑:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select email as principal, password as credentails, true from user where email=?")
.authoritiesByUsernameQuery("select user_email as principal, role_name as role from user_roles where user_email=?")
.passwordEncoder(passwordEncoder()).rolePrefix("ROLE_");
}
他/她实现了这些行来指定提交的用户凭据
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/register", "/", "/about", "/login", "/css/**", "/webjars/**").permitAll()
.antMatchers("/profile").hasAnyRole("USER,ADMIN")
.antMatchers("/users","/addTask").hasRole("ADMIN")
.and().formLogin().loginPage("/login").permitAll()
.defaultSuccessUrl("/profile").and().logout().logoutSuccessUrl("/login");
}
更具体地说,.formLogin().loginPage(“/login”).permitAll()
最后,让我们开始处理您的问题。这与我们无关。您需要创建提交到/login
的登录页面
关于的登录逻辑我在.jsp文件中放了一个HTML表单,结果是403无法验证提供的CSRF令牌,因为找不到您的会话。我在.jsp文件中放了一个HTML表单,结果是403无法验证提供的CSRF令牌,因为找不到您的会话。您可以使用.antMatchers(HttpMethod.POST,“/login”).permitAll()如果http方法为POST@gokhanbirincii添加使.jsp登录页面开始工作。问题是为什么thymeleaf不需要这段代码?这是关于csrf的情况。如果在configure方法中添加
http.csrf()
,则在发送put、delete、post
请求时必须发送csrf令牌。@Gokhanbircii这意味着thymeleaf自动删除/或添加http.csrf?不,thymeleaf既没有自动添加也没有自动删除csrf。您可以使用.antMatchers(HttpMethod.post,“/login”).permitAll()如果http方法是POST@gokhanbirincii添加使.jsp登录页面开始工作。问题是为什么thymeleaf不需要这段代码?这是关于csrf的情况。如果在configure方法中添加http.csrf()
,则在发送put、delete、post
请求时必须发送csrf令牌。@Gokhanbircii这意味着thymeleaf自动删除/或添加http.csrf?不,thymeleaf既没有自动添加也没有自动删除csrf。