如何使用Spring-Boot,MySQL和Thymeleaf更新已登录用户的用户详细信息?

时间:2020-10-10 13:45:43

标签: spring spring-boot spring-security thymeleaf

我需要在Spring Boot应用程序中添加一个“更新配置文件”页面。

这意味着当ROLE_USER登录到用户帐户时,该用户应该能够通过百里香叶形式更新MySql数据库中的详细信息。

我已经实现了ROLE_ADMIN的编辑详细信息功能。但这是完全不同的。在这种情况下,我不知道如何使用principal对象。

CustomerDetailsS​​ervice.java:

@Service
public class CustomerDetailsService implements UserDetailsService{
    
    @Autowired
    private CustomerRepository customerRepository;
   
    
    /*
     * Search function implementation for admin dashboard, customer details.
     */    
    public List<Customer> listAll(String keyword) {
        if (keyword != null) {
            return customerRepository.search(keyword);
        }
        return customerRepository.findAll();
    }
    


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Customer customer = customerRepository.findByEmailIdIgnoreCase(username);
        GrantedAuthority authority = new SimpleGrantedAuthority(customer.getRoleid());
    
        UserDetails userDetails = (UserDetails)new User(customer.getEmailId(),
                customer.getPassword(), Arrays.asList(authority));
        return userDetails;
    }
}

customerAccountEdit.html:(这是用于更新详细信息的表格。)

            <div class="wrapper" style="background-image: url('images/bg-registration-form-2.jpg');">
            <div class="inner">
                <form action="#" th:action="@{/saveuser}" method="post">
                    <h3>Edit Your Profile</h3>
                    <div class="form-group">
                        <div class="form-wrapper">
                            <label for="firstName">First Name</label>
                            <input th:field="*{firstName}" type="text" name="firstName" class="form-control" ></input>
                        </div>
                        <div class="form-wrapper">
                            <label for="lastName">Last Name</label>
                            <input th:field="*{lastName}" type="text" name="lastName" class="form-control" ></input>
                        </div>
                    </div>
                    <div class="form-wrapper">
                        <label for="dob">Date of Birth</label>
                        <input th:field="*{dob}" type="date" name="dob" class="form-control" ></input>
                    </div>
                    <div class="form-wrapper">
                        <label for="telephone">Telephone</label>
                        <input th:field="*{telephone}" type="text" name="telephone" class="form-control" ></input>
                    </div>
                    <div class="form-wrapper">
                        <label for="emailId">Email</label>
                        <input th:field="*{emailId}" type="email" name="emailId" class="form-control" ></input>
                    </div>
                    <div class="form-group">
                        <div class="form-wrapper">
                            <label for="street">Street</label>
                            <input th:field="*{street}" type="text" name="street" class="form-control" ></input>
                        </div>
                        <div class="form-wrapper">
                            <label for="city">City</label>
                            <input th:field="*{city}" type="text" name="city" class="form-control" ></input>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="form-wrapper">
                            <label for="district">District</label>
                            <input th:field="*{district}" type="text" name="district" class="form-control" ></input>
                        </div>
                        <div class="form-wrapper">
                            <label for="province">Province</label>
                            <input th:field="*{province}" type="text" name="province" class="form-control" ></input>
                        </div>
                    </div>
                    
                        <div class="form-wrapper">
                            <label for="password">Password</label>
                            <input th:field="*{password}" type="password" name="password" class="form-control" ></input>
                        </div>

                    <button>Save</button>               

                
                </form>
            </div>
        </div>

customerAccount.html :(这是成功登录后ROLE_USER的登录页面)

        <section class="product-filter-section">
        <div class="container">
            <div class="section-title">
                <h3>WELCOME TO YOUR USER-ACCOUNT</h3>   
                
                <form th:action="@{/app-logout}" method="post">
                <Input type="submit" class="myButton0" value="Sign Out"></Input>
                </form>
            </div>
            <div class="row">
                <div class="col-lg-3 col-sm-6">
                    <div class="product-item">
                        <div class="pi-pic">
                            <img src="./img/product/wishlist.jpg" alt=""></img>
                            <div class="pi-links">
                                <a href="#" class="add-card"><i class="flaticon-heart"></i><span>My Wishlist</span></a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-3 col-sm-6">
                    <div class="product-item">
                        <div class="pi-pic">
                            <img src="./img/product/promotions.jpg" alt=""></img>
                            <div class="pi-links">
                                <a href="#" class="add-card"><i class="flaticon-heart"></i><span>Promotions</span></a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-3 col-sm-6">
                    <div class="product-item">
                        <div class="pi-pic">
                            <img src="./img/product/tips.jpg" alt=""></img>
                            <div class="pi-links">
                                <a href="#" class="add-card"><i class="flaticon-heart"></i><span>Fashion Tips</span></a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-lg-3 col-sm-6">
                    <div class="product-item">
                        <div class="pi-pic">
                            <img src="./img/product/editprofile.jpg" alt=""></img>
                            <div class="pi-links">
                                <a href="/customerAccountEdit" class="add-card"><i class="flaticon-heart"></i><span>Edit Profile</span></a>
                            </div>
                        </div>
                    </div>
                </div>
                </div>
                </div>
                
    </section>

我不知道该怎么做。任何想法我该怎么办?预先感谢!

1 个答案:

答案 0 :(得分:0)

添加此依赖项后:

<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>

您可以使用sec:authorize,请参阅::

<div sec:authorize="hasRole('ROLE_ADMIN')">
  This content is only shown to administrators.
</div>
<div sec:authorize="hasRole('ROLE_USER')">
  This content is only shown to users.
</div>