-2
私はSpringブート+ Springセキュリティ+ Freemarkerを使用しています。Spring-boot Spring-security Freemarkerログイン成功404エラー
私は2つの部分の質問があります。
- 私のページに注入されたCSRFトークンに問題があります。私はフォームを使用することができないので、私はそれを取得しないと思う:フリーメーカのフォームタグ。
- csrfを無効にすると、ログイン成功後に404ページになります。私はApplicationSecurity.java
Application.java
@ComponentScan("in.co.mmbf.loanstar") @EnableAutoConfiguration public class Application extends SpringBootServletInitializer { public static void main(String[] args) throws Exception { new SpringApplicationBuilder(Application.class).run(args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } }
ApplicationConfig.java
@Configuration public class ApplicationConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { //registry.addRedirectViewController("/", "home"); registry.addViewController("/home").setViewName("home"); registry.addViewController("/login").setViewName("login"); registry.addViewController("/error").setViewName("error"); registry.addViewController("/admin").setViewName("admin"); } @Bean public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException { FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory(); factory.setTemplateLoaderPaths(new String[]{"classpath:org/springframework/web/servlet/view/freemarker/", "classpath:/templates/", "/templates"}); factory.setDefaultEncoding("UTF-8"); factory.setPreferFileSystemAccess(false); FreeMarkerConfigurer result = new FreeMarkerConfigurer(); result.setConfiguration(factory.createConfiguration()); return result; } }
の下にコピーされた設定
コードに間違ったsomewereと思う
@Configuration
@EnableWebSecurity
public class ApplicationSecurity extends WebSecurityConfigurerAdapter implements AuthenticationSuccessHandler {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/", "/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**", "/css/**", "/img/**", "favicon.ico");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
auth.inMemoryAuthentication().withUser("admin").password("password").roles("USER", "ADMIN");
}
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
if (roles.contains("ADMIN")){
response.sendRedirect("/Admin");
return;
}
response.sendRedirect("/home");
}
}
login.ftl
<!DOCTYPE html>
<html>
<head>
<title>MMBF Loan Star - Login</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.css" />
<link rel="stylesheet" href="css/custom/login.css">
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/bootstrap.js" type="text/javascript"></script>
<#import "/spring.ftl" as spring/>
</head>
<body onload="document.loginForm.username.focus();">
\t <div id="fullscreen_bg" class="fullscreen_bg"></div>
\t <div class="container">
\t \t <form class="form-signin" name="loginForm" action="/loanstar/login" method="post">
\t \t \t <h1 class="form-signin-heading text-muted">Sign In</h1>
\t \t \t <#if RequestParameters.error??>
\t \t \t \t <div class="alert alert-danger" align="center">
\t \t \t \t <strong>Invalid Login!</strong><br>Invalid username or password
\t \t \t \t </div>
\t \t \t <#elseif RequestParameters.logout??>
\t \t \t \t <div class="alert alert-info" align="center">
\t \t \t \t <strong>Logged out!</strong><br>You have Logged out of Loanstar
\t \t \t \t </div>
\t \t \t </#if>
\t \t \t
\t \t \t <input id="username" name="username" type="text" class="form-control" placeholder="Username" required autofocus>
\t \t \t <input id="password" name="password" type="password" class="form-control" placeholder="Password" required>
\t \t \t <button class="btn btn-lg btn-primary btn-block" type="submit">Sign In</button>
\t \t </form>
\t </div>
</body>
</html>
home.ftl
<#import "/layout/defaultLayout.ftl" as layout>
<@layout.pagelayout title="Home">
<div><h1>Hello Dude</h1></div>
</@layout.pagelayout>
server.contextPath=/loanstar
#security.basic.enabled=false
spring.mvc.favicon.enabled=false
spring.freemarker.template-loader-path=/
spring.freemarker.suffix=.ftl
をご覧ください。魅力的なように働いています。ありがとう@ chaoluo。 他人のために、私が間違っていたのはここです:(roles.contains場合 私は私のApplicationSecurity.java .loginPage( "/ログイン")にsuccessHandlerを追加する必要がありましたsuccessHandler(この) と 。 ( "ADMIN")){ \t redirectStrategy.sendRedirect(request、response、 "/ admin"); // response.sendRedirect( "/ Admin"); } else { \t redirectStrategy.sendRedirect(request、response、 "/ home"); // \t response.sendRedirect( "/ home"); } – Nivas