Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

java - How to use Spring Security to custom login page?

I am using Spring Security to my application and here is the security part which authenticates the user but the login page is given by Spring Security:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    public void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
            .authorizeRequests()
                .antMatchers("/home*").hasRole("USER")
                .and()
            .formLogin();

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                 .withUser("user")
                 .password("password")
                 .roles("USER")
    }
}

Instead of Spring's login page I want to use my login page which is below:

login.html:

<html lang='en'>
    <head>
        <title>WebApp</title>
        <meta charset='utf-8' />

        <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
             <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
             <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
             <link rel="stylesheet" href="css/login.css" />  
    </head>

    <body>
        <div class="login-page">
            <img src='img/taxi_.jpg' style='width: 180px; margin-left: auto; margin-right: auto; display: block; padding-top: 20px; padding-bottom:20px;' />

            <div class="heading">
                <center>Dashboard</center>
            </div>
            <div class="form">
                <form action ="home.html" class="register-form">
                    <input type="text" placeholder="name"/>
                    <input type="password" placeholder="password"/>
                    <input type="text" placeholder="email address"/>
                    <button>create</button>
                    <p class="message">Already registered? <a href="#">Sign In</a></p>
               </form>
               <form action="home.html" class="login-form">
                    <input type="text" placeholder="username"/>
                    <input type="password" placeholder="password"/>
                    <button id="button_login">login</button>
                    <p class="message">Not registered? <a href="#">Create an account</a></p>
               </form>
            </div>
        </div>
    </body>
</html>

How can I use my custom login page to be shown instead of Spring Security's login page?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just want to pinpoint a few moments that were not clarified here.

  1. First of all, .loginPage("/login.html") won't do the trick (it didn't in my case anyway). What is in quotes here is an URL path that will be searched for in your controller. Here is the snippet from my security config file:

     .formLogin().loginPage("/login")
     .loginProcessingUrl("/authentication").permitAll();
    

Here I wrote "/login". So now in your controller define that the /login path should point to your login.html page.

    @GetMapping("/login")
    public String login() {
        return "login";
    }

Only then it should redirect to the required view page. I usually place view pages under the /webapp/WEB-INF/view folder.
P.S. I hope you configured your ViewResolver bean right, cos otherwise it won't work :)

  1. And one more thing. I see you wanna use some CSS for the page. To enable CSS and other static resources for your custom login page, you should add this line

.antMatchers("/resources/**").permitAll()

So, in the end it will be like that (very short and creepy version, but your custom login should work):

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/resources/**").permitAll()
            .anyRequest().authenticated()
            .and()

        .formLogin().loginPage("/login")
        .loginProcessingUrl("/authentication").permitAll();
}

FYI, place your resources under /webapp/resources/. Also, you should configure your resource handler in your spring configuration file.

Here is also a nice link to wrap your head around it:

https://docs.spring.io/spring-security/site/docs/current/guides/html5/form-javaconfig.html#grant-access-to-remaining-resources


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...